1

我有两个代码块(也在另一个主题中描述)

- (void)AnimateImage:(NSString*)direction{
    self.CurrentAnimal.image = [images objectAtIndex:image_nr];
    CATransition *animation = [CATransition animation];
    [animation setDuration:1.0];
    [animation setType:kCATransitionPush];
    if([direction isEqualToString:@"left"]){
        [animation setSubtype:kCATransitionFromLeft];
    }
    else {
        [animation setSubtype:kCATransitionFromRight];
    }
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
    [[self.CurrentAnimal layer] addAnimation:animation forKey:nil];
}

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
    //do what you need to do when animation ends...
}

我知道我需要“设置委托属性并实现方法”来捕获 animationDidStop 事件,但我无法让它工作。

问题 - 如何设置我的 .h 和 .m 文件,以便在 CATransition 动画停止时能够使用此方法执行代码?

4

2 回答 2

1

设置委托:

CATransition *animation = [CATransition animation];
....
[animation setDelegate:self];
[animation setDuration:1.0];
[animation setType:kCATransitionPush];
于 2013-10-09T19:46:06.983 回答
1

您首先必须像这样添加DelegateCATransition您创建的对象中

CATransition *animation = [CATransition animation];
[animation setDelegate = self]; 
[animation setValue:animation.values.lastObject forKey:@"myAnimationKey"];

如果你为你的动画设置一个关键点,你可以准确地检查哪个动画结束了。如果您只有一个动画,则不需要特定的键。如果你设置一个键,你可以检查哪个动画像这样结束:

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

    if(anim == [animation.layer animationForKey:@"myAnimationKey"]){

    }
}
于 2013-10-09T19:55:52.990 回答