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

kkr*_*zka 55 iphone core-animation uiview ios4

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

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

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

我的问题是,iPhone OS 4.0中基于块的动画是什么?我虽然beginAnimations:context:和commitAnimations函数用于创建动画块..

ohh*_*hho 117

我在我的博客中发布了一个示例:

    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){
                            ;
                        }];

            }];
Run Code Online (Sandbox Code Playgroud)

上面的代码将在2秒动画中为UIImageView*(图标)设置动画.完成后,另一个动画会将图标移回原位.

  • 为无耻的自我推销+1(和一个很好的答案:) (18认同)

dra*_*ard 42

如果您按照该链接向上滚动一下,您将看到ios4新增的动画方法.

animateWithDuration:animations:
animateWithDuration:animations:completion:
animateWithDuration:delay:options:animations:completion:
Run Code Online (Sandbox Code Playgroud)

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

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

块对象并发编程的一部分


cld*_*drr 20

这是一个非常简单的例子.代码只淡出UIView并在动画完成后隐藏它:

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

或者以不同的格式:

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