完成块在setAnimationDelegate为self后停止工作

时间:2014-02-26 06:20:46

标签: ios objective-c animation uiview delay

我有一个基于UIView块的动画,如下所示:

 [UIView animateWithDuration:0.5
                          //sequential delays
                          delay:3.0 * (float)(index/25)
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         // set myself as delegate
                         [UIView setAnimationDelegate:self];
                         // when animation stared, play some sound
                         [UIView setAnimationWillStartSelector:@selector(playDrawCardSound)];
                         // some animation results here
                     }
                     completion:^(BOOL finished){
                         // this log will never be printed out.
                         NSLog(@"finished");
                     }];

所以,问题是所有的动画都很好,但完成处理程序永远不会被调用。

另外,我使用委托的原因是因为动画块不会作为动画延迟。话虽这么说,所有的声音都是一次播放的(不像动画片之间有延迟)。

有谁知道如何让动画块中的代码实际上被延迟?谢谢!

2 个答案:

答案 0 :(得分:0)

您不应该使用setAnimationDelegatesetAnimationWillStartSelector,因为文档说 在iOS 4.0及更高版本中不鼓励使用此方法。如果您使用基于块的动画方法,则可以直接在块中包含代理的起始代码。

在调用setAnimationDelegatesetAnimationWillStartSelector方法之间也必须调用beginAnimations:context:commitAnimations,这些方法在iOS 4.0及更高版本中不予采用。

可能不准确,但我认为这可能会有所帮助。

NSTimeInterval delay = 3.0 * (float)(index/25) ;
    [UIView animateWithDuration:0.5
     //sequential delays
                          delay:delay
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         [self performSelector:@selector(playDrawCardSound) withObject:nil afterDelay:delay] ;
                         // some animation results here
                     }
                     completion:^(BOOL finished){
                         // this log will never be printed out.
                         NSLog(@"finished");
                     }];

答案 1 :(得分:0)

我认为你不应该在UIViewAnimation中调用延迟方法,你可以试试这个:

NSTimeInterval delayInterval = 3.0 * (float)(index/25);
[self performSelector:@selector(playDrawCardSound) withObject:nil afterDelay:delayInterval];
[UIView animateWithDuration:0.5
                          //sequential delays
                          delay:delayInterval
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         // some animation results, 
                         //to do logic things in finish block or delay method above will be better
                     }
                     completion:^(BOOL finished){
                         // this log will never be printed out.
                         NSLog(@"finished");
                     }];

使用param尝试performSelector

[self performSelector:@selector(doDelayTast:) withObject:[NSDate date] afterDelay:4];

- (void)doDelayTast:(id)obj{
    NSDate *preDate = obj;
    NSLog(@"Delay timeinterval:%f", [[NSDate date] timeIntervalSinceDate:preDate]);
}

结果:

Delay timeinterval:4.066990

请注意,4s是发送邮件之前的最短时间,但在大多数情况下都可以忽略。