强制动画完成块在后台线程上运行到信号信号量

时间:2013-12-30 06:55:23

标签: ios objective-c multithreading animation objective-c-blocks

嗨所以我试图在动画完成块中发出信号量信号,但由于主线程被信号量阻塞,完成块甚至不会被触发。

无法在动画块中调用信号,因为它将很快被调用。

我尝试在后台线程中异步包装整个UIView动画块,但这并不妨碍将完成块分派到主队列。

-(void)performAnimation {
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);

    [UIView animateWithDuration:1.0 animations:^{
        // animation code   
    } completion: ^{
        dispatch_semaphore_signal(sema);
    }];

    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}

先谢谢你了!

1 个答案:

答案 0 :(得分:0)

我最后通过添加一个要传递给方法的完成块来解决它,这就是调用代码:

[AnimatorClass performAnimationwithCompletion:^{
    // code that should be ran after the animation
}

和新方法:

- (void)performAnimationwithCompletion:(void(^)(void))completion {
    [UIView animateWithDuration:1.0 animations:^{
        // animation code
    } completion:^(BOOL finished) {
        if (completion) {
            completion(); // now we can proceed to continue main thread execution
        }
    }];
}