不鼓励使用beginAnimations

Dus*_*tin 10 objective-c ios

meronix最近通知我,beginAnimations不鼓励使用.通过UIView类引用阅读我看到这确实是真的 - 根据Apple类参考:

在iOS 4.0及更高版本中不鼓励使用此方法.您应该使用基于块的动画方法来指定动画.

我看到很多其他方法 - 我经常使用 - 也"气馁",这意味着它们将会出现在iOS 6中(希望如此),但最终可能会被弃用/删除.

为什么不鼓励这些方法呢?

作为旁注,我现在正在使用beginAnimations各种应用程序,最常见的是在显示键盘时移动视图.

//Pushes the view up if one of the table forms is selected for editing
- (void) keyboardDidShow:(NSNotification *)aNotification
{
    if ([isRaised boolValue] == NO)
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.25];
        self.view.center = CGPointMake(self.view.center.x, self.view.center.y-moveAmount);
        [UIView commitAnimations];
        isRaised = [NSNumber numberWithBool:YES];
    }
}
Run Code Online (Sandbox Code Playgroud)

不确定如何使用基于块的方法复制此功能; 教程链接会很好.

wat*_*n12 19

他们气馁,因为有更好,更清洁的选择

在这种情况下,所有块动画都会自动将动画更改(setCenter:例如)包装在begin和commit调用中,这样您就不会忘记.它还提供了一个完成块,这意味着您不必处理委托方法.

Apple关于此的文档非常好,但作为一个例子,以块形式执行相同的动画

[UIView animateWithDuration:0.25 animations:^{
    self.view.center = CGPointMake(self.view.center.x, self.view.center.y-moveAmount);
} completion:^(BOOL finished){
}];
Run Code Online (Sandbox Code Playgroud)

此外,Ray wenderlich在块动画上有很好的帖子:链接

另一种方法是考虑块动画的可能实现

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
{
    [UIView beginAnimations];
    [UIView setAnimationDuration:duration];
    animations();
    [UIView commitAnimations];
}
Run Code Online (Sandbox Code Playgroud)