ben*_*ink 6 iphone core-animation core-graphics
我希望绘制一条曲线,直到它完全旋转并连接成一个完整的圆,只是圆形轮廓,没有填充.这必须在几秒钟内动画.
谁能指出我正确的方向?我已经问了一个类似的问题,但是我措辞不对,所以每个人都很难理解我的意思,因此它在问题的海洋中迷失了.
非常感谢您的帮助
[编辑]
我目前正在对UIView进行子类化并覆盖drawRect.我找到了绘制圆圈的代码,但我只需要笔画.
- (void)drawRect:(CGRect)rect {
// Drawing code
CGRect allRect = self.bounds;
CGRect circleRect = CGRectInset(allRect, 2.0f, 2.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
// Draw background
CGContextSetRGBStrokeColor(context, self.strokeValueRed, self.strokeValueGreen, self.strokeValueBlue, self.strokeValueAlpha); // white
CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 0.1f); // translucent white
CGContextSetLineWidth(context, self.lineWidth);
CGContextFillEllipseInRect(context, circleRect);
CGContextStrokeEllipseInRect(context, circleRect);
// Draw progress
CGPoint center = CGPointMake(allRect.size.width / 2, allRect.size.height / 2);
CGFloat radius = (allRect.size.width - 4) / 2;
CGFloat startAngle = - ((float)M_PI / 2); // 90 degrees
CGFloat endAngle = (self.progress * 2 * (float)M_PI) + startAngle;
CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f); // white
CGContextMoveToPoint(context, center.x, center.y);
CGContextAddArc(context, center.x, center.y, radius, startAngle, endAngle, 0);
CGContextClosePath(context);
CGContextFillPath(context);
}
Run Code Online (Sandbox Code Playgroud)
[编辑#2]
我更改了代码以删除所有填充引用,但现在它没有绘制任何东西:(任何想法?
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGRect allRect = self.bounds;
CGContextRef context = UIGraphicsGetCurrentContext();
// Draw background
CGContextSetRGBStrokeColor(context, self.strokeValueRed, self.strokeValueGreen, self.strokeValueBlue, self.strokeValueAlpha); // white
CGContextSetLineWidth(context, 5);
// Draw progress
CGPoint center = CGPointMake(allRect.size.width / 2, allRect.size.height / 2);
CGFloat radius = (allRect.size.width - 4) / 2;
CGFloat startAngle = - ((float)M_PI / 2); // 90 degrees
CGFloat endAngle = (self.progress * 2 * (float)M_PI) + startAngle;
CGContextAddArc(context, center.x, center.y, radius, startAngle, endAngle, 0);
CGContextStrokePath(context);
}
Run Code Online (Sandbox Code Playgroud)
[编辑#3]解决方案
对,我觉得自己是个合适的笨蛋!问题是,笔画颜色值没有初始化,这意味着该行正在绘制,但显然无法看到它!
Dav*_*ist 16
这和我在这里给你的另一个问题的答案是一样的.
你真正应该做的是为CAShapeLayer的笔画设置动画,其中路径是一个圆圈.这将使用Core Animation加速,并且在-drawRect:中绘制圆的一部分时不那么混乱.
下面的代码将在屏幕中心创建一个圆形图层,并顺时针为其设置动画,使其看起来好像正在绘制.你当然可以使用你想要的任何形状.(你可以阅读这篇文章上奥莱Begemanns博客,详细了解如何制作动画的形状层的行程.)
注意: stoke属性与图层上的边框属性不同.要更改笔划的宽度,您应该使用"lineWidth"而不是"borderWitdh"等.
// Set up the shape of the circle
int radius = 100;
CAShapeLayer *circle = [CAShapeLayer layer];
// Make a circular shape
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius)
cornerRadius:radius].CGPath;
// Center the shape in self.view
circle.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius,
CGRectGetMidY(self.view.frame)-radius);
// Configure the apperence of the circle
circle.fillColor = [UIColor clearColor].CGColor;
circle.strokeColor = [UIColor blackColor].CGColor;
circle.lineWidth = 5;
// Add to parent layer
[self.view.layer addSublayer:circle];
// Configure animation
CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration = 10.0; // "animate over 10 seconds or so.."
drawAnimation.repeatCount = 1.0; // Animate only once..
// Animate from no part of the stroke being drawn to the entire stroke being drawn
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
drawAnimation.toValue = [NSNumber numberWithFloat:1.0f];
// Experiment with timing to get the appearence to look the way you want
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
// Add the animation to the circle
[circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
Run Code Online (Sandbox Code Playgroud)
取出所有写着“填充”的东西。
将末尾的 CGContextFillPath 更改为 CGContextStrokePath。
取出靠近末尾的 CGContextMoveToPoint 和 CGContextClosePath。这些只是勾勒出楔子的直边。
您可能必须更改 CGContextMoveToPoint 以移动到圆弧的起点而不是中心,而不是将其取出。