Tag: semaphore

创build一个方法来执行animation,并在目标c中使用信号量等待完成

我想创build一个方法,使用UIView的“+ animateWithDuration:animation:完成”方法来执行animation,并等待完成。 我很清楚,我可以将通常会在完成块后出现的代码放在后面,但是我想避免这种情况,因为之后会有大量的代码,包括更多的animation,这会使我留下嵌套块。 我试图用一个信号量来实现这个方法,但是我不认为这是最好的方法,尤其是因为它实际上并不工作。 任何人都可以告诉我什么是我的代码错误,和/或达到相同目标的最佳方式是什么? +(void)animateAndWaitForCompletionWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations { dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); [UIView animateWithDuration:duration animations:animations completion:^(BOOL finished) { dispatch_semaphore_signal(semaphore); }]; dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); } 我不知道我的代码有什么问题,但是当我调用如下所示的方法时,完成块从来没有运行,我最终陷入了僵局。 [Foo animateAndWaitForCompletionWithDuration:0.5 animations:^{ //do stuff }]; – – – – – – – – – – – – – – – – – – – – – -编辑 – – […]