什么是iPhone OS 4.0中的基于块的animation方法?

我正在尝试使用iPhone OS 4.0(iOS4?)SDK来实现游戏。 在以前的SDK版本中,我一直使用[UIView beginAnimations:context:]和[UIView commitAnimations]来创build一些animation。 但是,当我查看4.0中的函数的文档时,我看到了这个评论。

在iPhone OS 4.0及更高版本中不鼓励使用此方法。 您应该使用基于块的animation方法。

你可以在这里find它: http : //developer.apple.com/iphone/library/documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/clm/UIView/commitAnimations

我的问题是,iPhone OS 4.0中基于块的animation是什么? 我虽然使用beginAnimations:context:和commitAnimations函数来创buildanimation块。

如果你按照这个链接向上滚动,你会看到ios4的新animation方法。

animateWithDuration:animations: animateWithDuration:animations:completion: animateWithDuration:delay:options:animations:completion: 

还有一些相关的过渡方法。 对于其中的每一个,animation参数都是一个块对象 :

animation
包含提交到视图的更改的块对象。 这是以编程方式更改视图层次结构中视图的任何animation属性的位置。 该块不带参数,没有返回值。 该参数不能为NULL。

块对象是并行编程的一部分

我在我的博客上发布了一个例子:

  CGPoint originalCenter = icon.center; [UIView animateWithDuration:2.0 animations:^{ CGPoint center = icon.center; center.y += 60; icon.center = center; } completion:^(BOOL finished){ [UIView animateWithDuration:2.0 animations:^{ icon.center = originalCenter; } completion:^(BOOL finished){ ; }]; }]; 

上面的代码将在一个2秒的animation中animationUIImageView *(图标)。 一旦完成,另一个animation将把图标移回原来的位置。

这是一个非常简单的例子。 该代码只是淡出一个UIView,并在animation完成后隐藏它:

 [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^ { bgDisplay.alpha = 0.0; } completion:^(BOOL finished) { bgDisplay.hidden = YES; }]; 

或以不同的格式:

 [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^ { bgDisplay.alpha = 0.0; } completion:^(BOOL finished) { bgDisplay.hidden = YES; }];