Iphone 设置圆动画';s端角与卡巴斯基化
我有一些UIView和drawrect代码用于绘制饼图:Iphone 设置圆动画';s端角与卡巴斯基化,iphone,ios,core-animation,cabasicanimation,Iphone,Ios,Core Animation,Cabasicanimation,我有一些UIView和drawrect代码用于绘制饼图: - (void)drawRect:(CGRect)rect { CGRect parentViewBounds = self.bounds; CGFloat x = CGRectGetWidth(parentViewBounds)/2; CGFloat y = CGRectGetHeight(parentViewBounds)/2; // Get the g
- (void)drawRect:(CGRect)rect
{
CGRect parentViewBounds = self.bounds;
CGFloat x = CGRectGetWidth(parentViewBounds)/2;
CGFloat y = CGRectGetHeight(parentViewBounds)/2;
// Get the graphics context and clear it
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextClearRect(ctx, rect);
// define line width
CGContextSetLineWidth(ctx, 4.0);
CGContextSetFillColor(ctx, CGColorGetComponents( [someColor CGColor]));
CGContextMoveToPoint(ctx, x, y);
CGContextAddArc(ctx, x, y, 100, radians(270), radians(270), 0); // 27
CGContextClosePath(ctx);
CGContextFillPath(ctx);
}
但当我尝试在绘制后设置“endAngle”属性的动画时,它不起作用:
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"endAngle"];
theAnimation.duration=1;
theAnimation.repeatCount=1;
theAnimation.autoreverses=NO;
theAnimation.fromValue=[NSNumber numberWithFloat:270];
theAnimation.toValue=[NSNumber numberWithFloat:60];
[[self.layer presentationLayer] addAnimation:theAnimation forKey:@"animateLayer"];
我在哪里犯了错误 如果要制作动画,则应研究如何为图形使用核心动画。它使动画更简单 看一看。我相信你可以修改它来得到你想要的
如果你觉得它很复杂,那么你可以通过画一个很宽的圆来看看哪个形状是饼图。有几件事。不能使用CAAnimation来设置在drawRect方法中所做的图形动画 其次,如果确实使用了CAShapeLayer,则可以设置对层中安装的路径所做更改的动画,但需要确保该路径在动画中的所有步骤都具有相同数量和类型的控制点 CGPath圆弧命令使用不同数量的控制点绘制不同的角度。因此,无法更改圆弧的角度并设置其动画 您需要做的是安装一条弧全长的路径,然后设置对形状层的strokeStart和/或strokeEnd属性所做更改的动画。这些特性会导致路径从图形中忽略路径的第一部分/最后一部分。我敢说David在可设置动画的饼图上链接的教程使用了这种技术