【问题标题】:setAnimationDidStopSelector: not getting calledsetAnimationDidStopSelector:没有被调用
【发布时间】:2012-07-03 16:37:50
【问题描述】:

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

-(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 都没有显示文本。我做错了什么?

【问题讨论】:

  • 为什么有两个beginAnimations
  • 对不起,这是一个错误,我已经从代码中删除了第一个。但问题依然存在。
  • 将这一行 [UIView beginAnimations:annKey context:NULL]; 移动到 moveImage:duration:curve:x:y:annKey: 方法的顶部。
  • 只是想知道,您不使用基于块的 UIView 动画调用是否有原因?

标签: objective-c ios xcode cocoa uiview


【解决方案1】:

你需要这样做 -

[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];

【讨论】:

    【解决方案2】:

    不确定问题出在哪里,但您确实不应该使用 beginAnimations/commitAnimations。相反,使用块动画。您直接在调用的完成部分定义完成代码。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 2019-03-31
      • 2012-12-16
      相关资源
      最近更新 更多