【问题标题】:How to implement animationdidstop如何实现动画停止
【发布时间】:2015-03-10 02:06:57
【问题描述】:

晚上好!

我的代码很平静,但它没有调用 animationdidstop 方法。我无法确定它为什么不起作用。我尝试了很多解决方案..

-(IBAction)MakeCircle:(id)sender{

 // Add to parent layer
[self.view.layer addSublayer:circle];

// Configure animation
drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration            = 5.0; 
drawAnimation.repeatCount         = 1.0;  
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
drawAnimation.toValue   = [NSNumber numberWithFloat:1.0f];
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

// Add the animation
[circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];}


-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{

if(anim == [self.view.layer animationForKey:@"drawCircleAnimation"]){
    Label.text = [NSString stringWithFormat:@"Loser"];
}

}

谢谢!!

【问题讨论】:

    标签: ios objective-c cabasicanimation caanimation


    【解决方案1】:

    您没有将自己设置为动画的代表。在添加动画之前添加这一行:

    drawAnimation.delegate = self;
    

    编辑:

    哦。我确切地知道问题是什么。当您提交动画时,系统会复制您的动画对象,因此在完成例程中它不会是同一个对象。尝试向动画添加唯一键并检查它,而不是检查它是否与您提交的动画对象相同。

    例如:

    drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    [drawAnimation setValue: @"mydrawCircleAnimation" forKey: @"animationKey"];
    //The rest of your animation code...
    

    然后在你的animationDidStop中:

    -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
    {
      if([anim valueForKey: @"animationKey" 
        isEqualToString: @"mydrawCircleAnimation"])
      {
          Label.text = [NSString stringWithFormat:@"Loser"];
      }
    }
    

    编辑#2:

    还有一种更简洁的方式来处理动画完成代码。您可以将一个块附加到您的动画,然后编写一个通用的 animationDidStop:completion: 方法来查找该块并在找到时调用它。在这个帖子中查看我的答案:

    How to identify CAAnimation within the animationDidStop delegate?

    【讨论】:

    • 你有没有试过在你的 animationDidStop:finished: 方法中添加一个日志语句?
    • 很好!!它有效.. IF 语句有问题.. 有什么想法吗?
    • 查看我的编辑。我应该更仔细地看看你的完成方法。
    • 语句 if 不起作用“没有可见的@interface”。
    猜你喜欢
    • 2016-03-27
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多