views:

65

answers:

2

Hi 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.

A: 

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];
brutella
Hi brutella and thanks. As I mention Apple discourages this approach as of 4.0. This is the way I have been doing stuff, and the thing I liked about this approach was that it was simple, could do multiple properties and was "readable". I can't figure how to get something similar using the CAAnimation, CABasicAnimation or CATransitions.
RickiG
+1  A: 

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.

lewiguez
Hi lewiguez and thanks, I love being told I am overthinking stuff:) It seems Apple didn't just dessert the simple animation stuff, but actually made it simpler. I found the "animating view with blocks" in the UIView reference you link to(I have been looking at the old reference and reading the old animation guide). So I guess going UIViewAnimation for a little while more, start out with blocks but have back-up code for 3.x and lastly, when the iPad and slow updaters are up to date, go all blocks. Great this I can use to move forward. Thanks again.
RickiG