在采取行动之前如何知道几个块是否已完成执行? [英] How to know if several blocks have completed execution before taking action?

查看:141
本文介绍了在采取行动之前如何知道几个块是否已完成执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using animateWithDuration:animations:completion: to move several elements of my User Interface (about 4 elements) before removeFromSuperview: is called.

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

My question is, how can I know that all those animations have completed before calling removeFromSuperview:?

推荐答案

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

Ok, to answer my own question.

我结束了做这样的事情:

I ended up doing something like this:

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

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

I hope this can serve as an example for someone else.

这篇关于在采取行动之前如何知道几个块是否已完成执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆