What are block-based animation methods in iPhone OS 4.0?

点点圈 提交于 2019-12-17 04:16:48

问题


I am trying to implement a game using the iPhone OS 4.0 (iOS4?) SDK. In the previous versions of the SDK, I've been using the [UIView beginAnimations:context:] and [UIView commitAnimations] to create some animations. However, when I look at the documentation of the functions in 4.0, I see this comment.

Use of this method is discouraged in iPhone OS 4.0 and later. You should use the block-based animation methods instead.

You can find it here: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/clm/UIView/commitAnimations

My question is, what are block-based animations in iPhone OS 4.0? I though that the beginAnimations:context: and commitAnimations functions were used to create animation blocks..


回答1:


If you follow that link and scroll up a bit, you will see animate methods new to ios4.

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

There are also some related transition methods. For each of these, the animations argument is a block object:

animations
A block object containing the changes to commit to the views. This is where you programmatically change any animatable properties of the views in your view hierarchy. This block takes no parameters and has no return value. This parameter must not be NULL.

Block objects are part of Concurrent Programming




回答2:


I have posted an example in my blog:

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

            }];

The above code will animate a UIImageView* (icon) in a 2-second animation. Once completed, another animation will move the icon back to it’s original position.




回答3:


Here's a very simple example. The code just fades out an UIView and hides it after the animation is done:

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

or in different formatting:

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


来源:https://stackoverflow.com/questions/3126833/what-are-block-based-animation-methods-in-iphone-os-4-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!