Objective c 设置UIView动画时阻止串行调度队列

Objective c 设置UIView动画时阻止串行调度队列,objective-c,ios,grand-central-dispatch,Objective C,Ios,Grand Central Dispatch,是否有方法手动阻止队列任务?我想在调度队列任务中使用UIView动画,但此任务应仅在动画完成后才能完成 dispatch_queue_t myCustomQueue; myCustomQueue = dispatch_queue_create("com.example.MyCustomQueue", NULL); dispatch_async(myCustomQueue, ^{ [UIView animateWithDuration:myDuration

是否有方法手动阻止队列任务?我想在调度队列任务中使用UIView动画,但此任务应仅在动画完成后才能完成

dispatch_queue_t myCustomQueue;
myCustomQueue = dispatch_queue_create("com.example.MyCustomQueue", NULL);

dispatch_async(myCustomQueue, ^{
    [UIView animateWithDuration:myDuration
                          delay:0.0f
                        options:0
                     animations:^{
                         // my changes here
                     }
                     completion:nil];
});

dispatch_async(myCustomQueue, ^{
    // if the animation from the task below is still running, this task should wait until it is finished...
});
  • 不要对主线程以外的任何对象进行UIView动画调用
  • 如果希望在动画完成后执行某些内容,请将其放入动画的完成块中。这就是它的目的
    使用
    dispatch\u Suspend
    挂起队列,然后在动画完成块中恢复队列(使用
    dispatch\u resume
    )。这将导致提交到队列的所有块在开始之前等待动画完成。请注意,当您挂起队列时,已在该队列上运行的块将继续运行。

    Swift 3中的问题

    我使用以下代码在Swift 3的主线程上执行。动画工作正常,但计时已关闭:

    // Animation works, timing is not right due to async
    DispatchQueue.main.async {
        // animation code
    }
    
    解决方案

    更新斯文对Swift 3的回答后,我能够使用以下代码使我的动画正常运行:

    DispatchQueue.main.suspend()
    // animation code goes here. 
    DispatchQueue.main.resume()
    

    1.我羞于忘记那件事。2.但问题是,在UIView动画开始时,我不知道下一个任务。在这种情况下,sven的答案是适合您的。我将从主线程运行动画,并在执行此操作时挂起操作队列。所以这是你两个答案的结合。谢谢