2012-08-01 43 views
5

我使用animateWithDuration:animations:completion:removeFromSuperview:之前搬走我的用户界面的几个要素(约4元)被调用。如何在采取行动之前知道是否有几个块已经完成执行?

我的问题是,我怎么能知道所有这些动画都调用removeFromSuperview:前完成?

+0

无法您在动画节同时它们的动画所有,而不是创建多个块的? – 2012-08-01 22:35:56

+0

若动画具有不同的持续时间和风格的 – Goles 2012-08-01 22:41:21

+0

可能重复的[如何调用单个完成块的UIView animateWithDuration的嵌套组呼叫?](http://stackoverflow.com/questions/8421441/how-to-invoke -a-single-completion-block-for-a-nested-group-of-uiview-animatewith) – 2012-08-01 22:42:08

回答

9

好吧,回答我的问题。

最后我做这样的事情:

// Create dispatch queue & group 
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
    dispatch_group_t group = dispatch_group_create(); 

    // Two group enters 
    dispatch_group_enter(group); 
    dispatch_group_enter(group); 

    // (notice the group parameter in dispatch_group_leave) 
    [UIView animateWithDuration:0.3 animations:^{ 
     self.pickerView.frame = CGRectMake(self.pickerView.frame.origin.x 
              , self.view.bounds.size.height + self.pickerView.frame.size.height/2 
              , self.pickerView.frame.size.width 
              , self.pickerView.frame.size.height); 
    } completion:^(BOOL finished){ 
     dispatch_group_leave(group); 
    }]; 


    [UIView animateWithDuration:0.3 animations:^{ 
     self.navigationBar.frame = CGRectMake(self.navigationBar.frame.origin.x 
               , -self.navigationBar.frame.size.height 
               , self.navigationBar.frame.size.width 
               , self.navigationBar.frame.size.height); 
    } completion:^(BOOL finished){ 
     dispatch_group_leave(group); 
    }]; 

    // Finishing callback 
    dispatch_group_notify(group, queue, ^{ 
     [self.view removeFromSuperview]; 
    }); 

    // Release the group 
    dispatch_release(group); 

我希望这可以成为别人的例子。

+2

将此添加为常规答案。 – 2013-07-19 01:20:17

0

创建调度队列,由你正在做动画的数量挂起。将一个块添加到从superview中删除的队列中。在每个动画的完成块中恢复暂停的队列。当最后一个完成时,排队的块将运行。

+0

你能详细说明一些代码吗? – Goles 2012-08-01 23:56:53

1

您也可以使用CATransaction。它会捕捉任何嵌入的动画:

[CATransaction begin]; 
[CATransaction setCompletionBlock:^{ // all animations finished here }]; 
[UIView animateWithDuration... 
[UIView animateWithDuration... 
... 
[CATransaction commit]; 

这将允许你不必跟踪自己的动画。

相关问题