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

标题: ios - CABasicAnimation + UIBezierPath [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 03:45
标题: ios - CABasicAnimation + UIBezierPath

我正在尝试创建一个圆形条,当您进行延时摄影时,您可以在 iOS 原生相机的录制按钮周围找到它。

沿着动画创建一个圆圈,一旦完成,就会“自然”地再次删除。

我尝试了下一个代码:

CAShapeLayer *circle = [CAShapeLayer layer];
circle.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.lapseBtnOutlet.center.x, self.lapseBtnOutlet.center.y) radius:28 startAngle:2*M_PI*0-M_PI_2 endAngle:2*M_PI*1-M_PI_2 clockwise:YES].CGPath;
circle.fillColor = [UIColor clearColor].CGColor;
circle.strokeColor = [UIColor whiteColor].CGColor;
circle.lineWidth = 2.0;

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath"strokeEnd"];
animation.duration = self.lapseInterval;
animation.removedOnCompletion = NO;
animation.fromValue = @(0);
animation.toValue = @(1);
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[circle addAnimation:animation forKey"drawCircleAnimation"];

但我现在的问题是我不知道如何从头到尾“删除”这条线。

我尝试使用 autoreverse 属性,但它从头到尾删除了圆,而不是从头到尾。有任何想法吗?



Best Answer-推荐答案


您需要将 strokeStart 的动画从 0 变为 1,当动画结束时,移除形状图层。

- (void)viewDidLoad {
    [super viewDidLoad];
    [self performSelectorselector(animateProperty withObject"strokeEnd" afterDelay:1];
    [self performSelectorselector(animateProperty withObject"strokeStart" afterDelay:3];
}


-(void)animatePropertyNSString *) prop {
    if (! self.circle) {
        self.circle = [CAShapeLayer layer];
        self.circle.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.lapseBtnOutlet.center.x, self.lapseBtnOutlet.center.y) radius:28 startAngle:2*M_PI*0-M_PI_2 endAngle:2*M_PI*1-M_PI_2 clockwise:YES].CGPath;
        self.circle.fillColor = [UIColor clearColor].CGColor;
        self.circle.strokeColor = [UIColor whiteColor].CGColor;
        self.circle.lineWidth = 2.0;
        [self.view.layer addSublayer:self.circle];
    }

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:prop];
    animation.delegate = ([prop isEqualToString"strokeStart"])? self : nil;
    animation.duration = 1;
    animation.removedOnCompletion = NO;
    animation.fromValue = @0;
    animation.toValue = @1;
    [self.circle addAnimation:animation forKey:prop];
}


-(void)animationDidStopCAAnimation *)anim finishedBOOL)flag {
    [self.circle removeFromSuperlayer];
    self.circle = nil;
}

关于ios - CABasicAnimation + UIBezierPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27323006/






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