OStack程序员社区-中国程序员成长平台

标题: iphone - 使用 CABasicAnimation 动画圆的 endAngle [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 16:45
标题: iphone - 使用 CABasicAnimation 动画圆的 endAngle

我有一些带有 drawrect 代码的 UIView 用于绘制饼图:

    - (void)drawRectCGRect)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"];

我在哪里犯了错误?



Best Answer-推荐答案


如果你想制作动画,那么你应该考虑使用 Core Animation 进行绘图。它使动画更简单。

看看this great tutorial on making a custom animatable pie chart with Core Animation .我相信你可以修改它以获得你想要的。

如果您觉得这很复杂,那么您可以查看 my answer"Draw part of a circle"它通过画一个非常宽的圆圈来绘制饼图形状。

关于iphone - 使用 CABasicAnimation 动画圆的 endAngle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12787582/






欢迎光临 OStack程序员社区-中国程序员成长平台 (http://ostack.cn/) Powered by Discuz! X3.4