views:

679

answers:

1

I'm trying to animate a view sliding into view and bouncing once it hits the side of the screen.

A basic example of the slide I'm doing is as follows:

// The view is added with a rect making it off screen.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.07];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[theView setFrame:CGRectMake(-5, 0, theView.frame.size.width, theView.frame.size.height)];
[UIView commitAnimations];

More animations are then called in the didStopSelector to make the bounce effect. The problem is when more than one view is being animated, the bounce becomes jerky and, well, doesn't bounce anymore.

Before I start reading up on how to do this in Core Animation, (I understand it's a little more difficult) I'd like to know if there is actually an advantage using Core Animation rather than UIView animations. If not, is there something I can do to improve performance?

+1  A: 

With Core Animation (Layer based) you can do much advanced things, like 3d animation (embedding your view in 3d space).

UiView animations are quite powerful though, and shouldn't be a performance bottleneck until you have plenty of them. I only see one example in your code, and its duration is extremely short. In fact, you are almost setting the frame property instantly (70 ms is 14 fps). You should think of UIView animation in terms of start, end, and the duration for the whole distance - which is typically 0.2 s to several seconds. It is not designed to to fire one micro animation after another.

Eiko
There are a few extremely short animations following the example given, if they're not designed for this kind of thing, what should I use instead to give a bounce like effect?
Tom Irving
I wouldn't give up too early on UIViews and their animation. Just consider if an animation makes sense. If you move a view within 0.07 seconds, your animation is no real animation anyway - as there are almost no positions to interpolate between start and end. How should anyone be able to follow that animation? If it is part of a longer linear movement, then animate the whole thing as one animation. Also, there is a repeatCount property, which might or might not be useful in your scenario. Point is, 0.07 s is no resonable timespan for any animation. Maybe posting the whole sequence is useful.
Eiko
You're right, 0.07 seconds is quick, very quick, but there is definitely a difference visually between using a very quick animation and no animation at all. I think my question at this point is should 3 or 4 simultaneous very quick animations cause jerkiness, regardless of speed?
Tom Irving
It may or it may not - I cannot tell. A couple of parallel animations should not hurt. Spawning new animations every couple of milliseconds might. You can always profile and see if anything else consumes your CPU time. If the main thread is blocked due to heavy computation this might explain it. Also be sure to test on the slowest device you will target, as performance on of first generation iPods is quite different from the latest iPads.
Eiko