首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何重置CABasicAnimation?

如何重置CABasicAnimation?
EN

Stack Overflow用户
提问于 2015-05-24 12:55:40
回答 1查看 398关注 0票数 0

我有一个动画,它是一个酒吧,它通过一个计时器在屏幕上传播,每次点击时,计时器都会缩短时间。我的问题是动画只播放一次。我试着调用动画来重置并重新开始条形图,但是什么也没有发生。以下是一些代码:

代码语言:javascript
复制
-(IBAction)targetTapped:(id)sender {
    [Time invalidate];

    targX = arc4random() %619;
    targY = arc4random() %925;

    NSLog([NSString stringWithFormat:@"X = %li", (long)targX]);
    NSLog([NSString stringWithFormat:@"Y = %li", (long)targY]);

    Target.center = CGPointMake(targX, targY);

    Score = Score + 1;

    if (Score >= 225) {
        timeMax = 0.5;
    }
    else {
        timeMax = 5 - (Score * 0.02);
    }

    Time = [NSTimer scheduledTimerWithTimeInterval:timeMax target:self selector:@selector(Lose) userInfo:nil repeats:NO];

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0.0,895.0)];
    [path addLineToPoint:CGPointMake(768.0, 895.0)];

    CAShapeLayer *pathLayer = [CAShapeLayer layer];
    pathLayer.frame = self.view.bounds;
    pathLayer.path = path.CGPath;
    pathLayer.strokeColor = [[UIColor blueColor] CGColor];
    pathLayer.fillColor = nil;
    pathLayer.lineWidth = 5.0f;
    pathLayer.lineJoin = kCALineJoinBevel;

    [self.view.layer addSublayer:pathLayer];

    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = timeMax;
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
    [pathLayer animationForKey:@"strokeEnd"];
    [pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-24 13:43:24

您可以创建一次pathLayer:

代码语言:javascript
复制
[self.pathLayer removeAnimationForKey:@"strokeEnd"];

if (self.pathLayer == nil) {
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0.0,895.0)];
    [path addLineToPoint:CGPointMake(768.0, 895.0)];
    CAShapeLayer *pathLayer = [CAShapeLayer layer];
    pathLayer.frame = self.view.bounds;
    pathLayer.path = path.CGPath;
    pathLayer.strokeColor = [[UIColor blueColor] CGColor];
    pathLayer.fillColor = nil;
    pathLayer.lineWidth = 5.0f;
    pathLayer.lineJoin = kCALineJoinBevel;

    [self.view.layer addSublayer:pathLayer];

    self.pathLayer = pathLayer;
}

CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = timeMax;
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
[self.pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30423730

复制
相关文章

相似问题

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