【问题标题】:CABasicAnimation + UIBezierPathCABasicAnimation + UIBezierPath
【发布时间】:2014-12-05 19:09:38
【问题描述】:

我正在尝试创建一个圆形条,当您进行延时摄影时,您可以在 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 属性,但它从头到尾删除了圆,而不是从头到尾。有任何想法吗?

【问题讨论】:

    标签: ios uibezierpath cabasicanimation


    【解决方案1】:

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

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self performSelector:@selector(animateProperty:) withObject:@"strokeEnd" afterDelay:1];
        [self performSelector:@selector(animateProperty:) withObject:@"strokeStart" afterDelay:3];
    }
    
    
    -(void)animateProperty:(NSString *) 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)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
        [self.circle removeFromSuperlayer];
        self.circle = nil;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多