如何实现动画停止
How to implement animationdidstop
晚上好!
我有一个安静的代码,但它没有调用 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"];
}
}
谢谢!!
您没有将自己设置为动画代理。在添加动画之前添加这一行:
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 方法。我无法确定为什么它不起作用。我尝试了很多解决方案..
-(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"];
}
}
谢谢!!
您没有将自己设置为动画代理。在添加动画之前添加这一行:
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?