首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CABasicAnimation + UIBezierPath

CABasicAnimation + UIBezierPath
EN

Stack Overflow用户
提问于 2014-12-05 19:09:38
回答 1查看 1.4K关注 0票数 2

我正在尝试创建一个圆形条形图,当您执行时间间隔时,您可以在iOS上的本机摄像机中的录制按钮周围找到这样的条。

一个圆圈是沿着动画创建的,一旦它完成,就会“自然”地移除。

我尝试了下一个代码:

代码语言:javascript
复制
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"];

但我现在的问题是,我不知道如何“删除”线从开始到结束。

我尝试了自动反转属性,但它删除了从端到开始的循环,而不是从开始到结束。有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-05 20:58:17

您需要将strokeStart从0动画变为1,当动画完成时,删除形状层。

代码语言:javascript
复制
- (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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27323006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档