2012-10-21 38 views
2

有沒有辦法手動阻止隊列任務?我想在一個調度隊列任務中使用UIView動畫,但是這個任務只應該在動畫完成時完成。在動畫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... 
}); 

回答

4

使用dispatch_suspend掛起隊列,然後在動畫完成塊中恢復它(使用dispatch_resume)。這將導致提交給隊列的所有塊在啓動之前等待動畫完成。請注意,掛起時已在該隊列上運行的塊將繼續運行。

2
  1. 如果你想要一個動畫完成後的東西來執行,把它放在動畫的完成塊不要超過主線程
  2. 其他任何使UIView的動畫調用。它就是這樣的。
+0

1.我很慚愧地忘記了這一點。 2.但是,這是問題,在UIView動畫開始的時候,我不知道下一個任務。 – miho

+0

在這種情況下,斯文的答案就是你的答案。 – jrturton

+0

我將從主線程運行動畫,並在執行操作時暫停動作隊列。所以這是你的答案的組合。謝謝。 – miho

0

問題在夫特3

我用下面的代碼來在夫特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()