iPhone - 使用animateWithDuration而不是UIView beginAnimations有什么优势吗?

Spa*_*Dog 1 iphone ios objective-c-blocks

一个简单的问题:

这是旧时尚动画的一个例子:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];

[base setTransform:rotate];
[base setCenter:newCenter];

[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

这可以写成

[UIView animateWithDuration:0.5 animations:^{

[base setTransform:rotate];
[base setCenter:newCenter];

}];
Run Code Online (Sandbox Code Playgroud)

使用这个新表单重写动画有什么好处吗?

应该会有某种收获,或Apple不会发挥这种新功能.

你们说什么?

lxt*_*lxt 7

Apple改变的不是性能,而是因为块是一种表达这种东西的简单方法.以前,您必须使用选择器在动画完成时触发,等等.

所以 - 为什么要使用animateWithDuration:因为块可以节省时间,使代码更清晰,而且通常非常有用.

为什么要使用beginAnimation:因为你想支持4.0之前的iOS版本,那里的代码不可用.Apple仍然需要提供这两种方法,因为它们需要保持向后兼容 - 但文档强烈建议您在可用且适当的情况下使用块版本的方法.