![](/img/trans.png)
[英]Difference between [UIView beginAnimations:context:] and [UIView animateWithDuration:animations:]
[英]iPhone - is there any advantage in using animateWithDuration instead of UIView beginAnimations?
一個簡單的問題:
這是一個老式時尚動畫的示例:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[base setTransform:rotate];
[base setCenter:newCenter];
[UIView commitAnimations];
這可以寫成
[UIView animateWithDuration:0.5 animations:^{
[base setTransform:rotate];
[base setCenter:newCenter];
}];
使用這種新形式重寫動畫有什么優勢嗎?
應該會有一些收獲,否則蘋果將無法實現這一新功能。
你們怎么說
蘋果所做的改變不是為了性能,而是因為塊是表達這種事情的一種更簡單的方法。 以前,您必須使用選擇器在動畫結束時觸發等。
所以-為什么要使用animateWithDuration
:因為塊節省了時間,使代碼更整潔,並且通常非常有用。
以及為什么要使用beginAnimation
:因為您要支持4.0之前的iOS版本,因此該代碼不可用。 Apple仍需要提供這兩種方法,因為它們需要保持向后兼容-但是文檔強烈建議您在可用且適當的地方使用方法的塊版本。
我認為animateWithDuration較新並且外觀更好。 我使用它的方式比beginAnimation多 。 它是更清晰的代碼。 當您需要與低於4.0的iOS版本兼容時使用beginAnimation 。
但在某些情況下,beginAnimation有更多的優勢 ,能夠更方便,當你寫與動畫參數的函數。 例:
- (void)moveSomethingWithAnimated:(BOOL)animated {
// Do other task 1
if( animated ) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
someView.frame = newFrame;
otherView.frame = newFrame;
}
if( animated ) {
[UIView commitAnimations];
}
// Do other task 2
}
代替:
- (void)moveSomethingWithAnimated:(BOOL)animated {
// Do other task 1
if( animated ) {
[UIView animateWithDuration:0.2 animations:^{
someView.frame = newFrame;
otherView.frame = newFrame;
}];
}
else {
// duplicate code, or you have to write another function for these two line bellow
someView.frame = newFrame;
otherView.frame = newFrame;
}
// Do other task 2
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.