如何实现animationdidstop

时间:2015-03-10 02:06:58

标签: ios objective-c cabasicanimation caanimation

晚安!

我有一个代码和平,但它没有调用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"];
}

}

谢谢!

1 个答案:

答案 0 :(得分:0)

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

drawAnimation.delegate = self;

编辑:

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

e.g:

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?