[英]Sequential iOS animation *without* blocks?
在不使用塊結構的情況下如何為UIView設置動畫? 我只需要強制動畫在調用后立即執行,就沒有足夠的精力將我的程序的其余部分放在基於代碼結構的完成塊中。
具體來說,現在正在發生什么
while (actionsRemaining)
{
[self performDesiredAnimation];
....computation ....
[barWidget decreaseValueTo:resultOfComputation];
}
當我運行此代碼時,所有[performDesiredAnimation]動畫會同時發生,而10個左右的barWidgets會同時減少。 我想要的是:
performDesiredAnimation1-> barWidget減少-> performDesiredAnimation2-> barWidget減少-> performDesiredAnimation3-> barWidget減少-> ...但是很多未知的時間。
我不知道有多少次,因為動作將從動作中刪除。如果barWidget減少到某個值以上,則剩余,即循環數將取決於中間計算。
簡而言之,我想要的結果與我現在只調用循環的一次迭代時看到的結果相同
[self performDesiredAnimation];
....computation ....
[barWidget decreaseValueTo:resultOfComputation];
,然后進行第二次迭代,再進行第三次迭代,所有過程都是孤立的。 現在,如果我注釋掉循環,動畫看起來就不錯了,但是當我將循環保留在那里時,它們都粉碎了。 我只希望動畫在迭代之間順序執行,而不是一次全部執行。
障礙不是導致您出現問題的原因。 我認為您的問題是您的“ for循環動畫”結構。
您需要將動畫動作保存在堆棧中,當一個動作完成時,將下一個動作彈出堆棧並執行。 或者,如果需要繼續,請在動畫的完成塊中簽入,如果需要,請再次調用動畫方法。 就像是:
-(void)performAnimation
{
[UIView animateWithDuration:0.25
animations:^{[self performDesiredAnimations];}
completion:^(BOOL finished){
// It's not clear what your computations are or if they take any significant time
CGFloat result = [self doComputations];
[barWidget decreaseValueTo:resultOfComputation]
if (needToPerformAnotherSet) // Work this out however you need to
{
[self performAnimation];
}
};
}
你可以這樣做:
// Start the work in a background thread.
dispatch_async(dispatch_get_global_queue(0, 0), ^{
while (actionsRemaining) {
// Do your calculation in the background if your code will allow you to
[self performDesiredAnimation];
....computation ....
// Back to the main thread for a chunk of code
dispatch_sync(dispatch_get_main_queue(), ^{
// Or do it here if not
[self performDesiredAnimation];
....computation ....
// Finally, update your widget.
[barWidget decreaseValueTo:resultOfComputation];
}
}
}
}
由於您的循環會在每次迭代中進入異步后台循環,因此應用程序的主運行循環將有機會使用您在sync'd塊中設置的值來更新您的UI。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.