setAnimationDidStopSelector:没有被调用

时间:2012-07-03 16:37:50

标签: objective-c ios xcode cocoa uiview

我知道这个问题被问了很多,但我已经尝试了所有其他答案,我无法弄清楚为什么动画停止选择器不被调用。这是代码:

-(void) moveImage:(UIImageView *)image duration:(NSTimeInterval)duration curve:(int)curve  x:(CGFloat)x y:(CGFloat)y annKey:(NSString *) annKey{  

[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];
CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
image.transform = transform;
[UIView beginAnimations:annKey context:NULL];
[UIView commitAnimations];

}

这是发送所有正确参数的主要动画功能。

- (void)animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context
{
if ([animationID isEqualToString:@"Burn"])
{
    NSLog(@"Animation: %@ has finished.",animationID);
}

NSLog(@"This does not get called, why not?");
}

我的NSLog都没有显示文字。我做错了什么?

2 个答案:

答案 0 :(得分:8)

你需要做 -

[UIView beginAnimations:annKey context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];

// Now define rest.
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
[UIView setAnimationBeginsFromCurrentState:YES];
CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
image.transform = transform;

[UIView commitAnimations];

答案 1 :(得分:3)

不确定问题是什么,但你真的不应该使用beginAnimations / commitAnimations。相反,使用块动画。您可以直接在呼叫的完成部分中定义完成代码。

[UIView animateWithDuration:2.0
                      delay:0.0
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{
                     // animation code
                 } 
                 completion:^(BOOL finished){
                     // completion code
                 }];