开发者

Animation in Cocoa Touch?

I have been using UIViewAnimation for simple animations i the past, now I can see that Apple discourages the use of this Class in +4.0. I started looking into CATransitions and -animation. It gives some more fine grained control but seem really bloated for my limited use. When I try to get a grip of the possibilities on the platform I run into a rabbit hole with tons of stuff on Core Animation, Quartz and OPEN GLS, but no simple stuff on animation, to get me started.

I need to "tween" the position, alpha, scale and rotation, sometimes all at once on a few UIViews and have a delegate hook for animationDidStart and didStop. Are there any good Tween engines out there? having dealt with flash and Java it is really one of things I miss.

If I use CATransitions it seems I am limited to animating one property at a time, i.e. alpha, if I then have to move and 开发者_运维知识库rotate the UIView simultaneous I will have to set up individual animations for each property. If I should go with CATransition, is there a way to build an animation object that animates several properties at once?

I would be really grateful if someone can get me started on a way where the complexity of the animation is proportional to the amount of code needed:) Thanks in advance.


You're overthinking this.

While Apple does discourage the beginAnimations:context: methods in iOS 4, they provided a WONDERFUL new way to do this that is even easier than before. It's still part of the UIView class, it just uses a great new feature provided with the update: blocks.

In fact, Apple even encourage using block-based animations instead. Taken from the UIView class reference:

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

Block-based animations are really simple to use. They take the form of the following:

[UIView animateWithDuration:5
   animations:^{
       self.yourView.frame = CGRectMake(100, 100, 100, 200);
       self.yourView.alpha = 0.3;
   }
   completion:^(BOOL finished){
       self.yourView.alpha = 1.0;
       [self.yourProperty doSomethingReallyCool];
   }
];

Look familiar? It's very similar to the beginAnimations:context: method. The little ^{} section is the block. Blocks maintain state, so they work pretty well for this kind of thing. For more information about blocks, check out

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html.

Make sure you view the UIView class reference located here. There are a number of new block-based animation methods.

Also, it's worth noting that block are iOS 4 only. So, provided back-up code for SDK 3 or lower devices. Or, just keep using beginAnimations:context: for now, but be aware its days are numbered.


You can set the properties which should be animated within the [UIView beginAnimations:nil context:nil]; method like:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];

view.alpha = 0.0f;
view.center = CGPointMake(34.0f,23.0f);
...

[UIView commitAnimations];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜