在采取行动之前如何知道几个街区是否已经完成执行?

我正在使用animateWithDuration:animations:completion:removeFromSuperview:之前移动我的用户界面的几个元素(大约4个元素) removeFromSuperview:被调用。

我的问题是,在调用removeFromSuperview:之前,如何知道所有这些animation已经完成?

好的,回答我自己的问题。

我最终做了这样的事情:

  // 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); 

我希望这可以作为别人的榜样。

您也可以使用CATransaction。 它会捕捉任何embedded的animation:

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

这可以让你不必跟踪自己的animation。

创build一个调度队列,暂停它正在做的animation的数量。 将一个块添加到从superview中删除的队列中。 在每个animation的完成块中恢复暂停的队列。 当最后一个完成时,排队的块将运行。