不鼓励使用beginAnimations

时间:2012-07-30 14:26:55

标签: 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];
    }
}

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

2 个答案:

答案 0 :(得分:19)

他们气馁,因为有更好,更清洁的替代方案

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

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

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

此外,Ray wenderlich在块动画上有一个很好的帖子:link

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

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
{
    [UIView beginAnimations];
    [UIView setAnimationDuration:duration];
    animations();
    [UIView commitAnimations];
}

答案 1 :(得分:1)

在UIView上查看this method,这非常简单。现在最棘手的部分是不允许块有一个强大的指针指向self:

//Pushes the view up if one of the table forms is selected for editing
- (void) keyboardDidShow:(NSNotification *)aNotification
{
  if ([isRaised boolValue] == NO)
  {
    __block UIView *myView = self.view;
    [UIView animateWithDuration:0.25 animations:^(){
      myView.center = CGPointMake(self.view.center.x, self.view.center.y-moveAmount);
     }];
    isRaised = [NSNumber numberWithBool:YES];
  }
}
相关问题