Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

objective c - Use of beginAnimations discouraged

I was informed recently by meronix that use of beginAnimations is discouraged. Reading through the UIView class reference I see that this is indeed true - according to the Apple class ref:

Use of this method is discouraged in iOS 4.0 and later. You should use the block-based animation methods to specify your animations instead.

I see that a large number of other methods - which I use frequently - are also "discouraged" which means they'll be around for iOS 6 (hopefully) but probably will be deprecated/removed eventually.

Why are these methods being discouraged?

As a side note, right now I'm using beginAnimations in all sorts of apps, most commonly to move the view up when a keyboard is shown.

//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];
    }
}

Not sure how to duplicate this functionality with block-based methods; a tutorial link would be nice.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

They are discouraged because there is a better, cleaner alternative

In this case all a block animation does is automatically wrap your animation changes (setCenter: for example) in begin and commit calls so you dont forget. It also provides a completion block, which means you don't have to deal with delegate methods.

Apple's documentation on this is very good but as an example, to do the same animation in block form it would be

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

Also ray wenderlich has a good post on block animations: link

Another way is to think about a possible implementation of block animations

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...