2017-05-15 44 views
0
[UIView animateWithDuration:1 animations:^{ 
    self.animateView.frame = CGRectMake(100, 100, 50, 50); 
} completion:^(BOOL finished) { 
    if (finished) { 
     NSLog(@"animation finished"); 
    } 
}]; 
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]]; 
NSLog(@"after runUntilDate"); 

像上面這麼多代碼,那是爲什麼?如果我評論runUntilDate行,會發生什麼?爲什麼需要在動畫語句後添加runUntilDate?

更新: 如果我將下一行(NSLog)添加到最後一行,它將不會執行,直到動畫結束。看起來runUntilDate阻塞主線程,所以我們爲什麼需要在這裏阻塞線程?

回答

0

在你的情況下,我認爲,什麼都沒有。請參閱here

+0

爲什麼runUntilDate可以阻塞主線程?而當我們需要阻塞主線程時就像這裏一樣? – Sugite

+0

因爲runloop在主線程中工作。理想情況下,您不能在任何情況下阻止主線程。 – SerjOxo

+0

runUntilDate只需重新啓動runloop的另一個迭代,爲什麼導致線程阻塞,你可以在源代碼級別解釋嗎?謝謝! – Sugite

相關問題