Queuing Animations

61 views
Skip to first unread message

Peter Robinett

unread,
Jan 22, 2014, 1:03:57 PM1/22/14
to cocoah...@googlegroups.com
Hi all,

I'm working on a project where we have an annoying issue related to animations and I'd love to get your suggestions.

Simply: we're doing an animation in response to events. If a second event occurs while the first animation is in progress, I'd like to cancel the first and and start the second, since it has the most up-to-date data. 

How would you do this? An NSOperationQueue? Would you add CATransaction to the mix?

I personally have no experience with animations, so I'd love some tips.

Thanks,
Peter

Christian Apers

unread,
Jan 22, 2014, 1:47:05 PM1/22/14
to cocoah...@googlegroups.com
Hi Peter,

I probably need more context, but I'll try to write something useful (if you are using Core Animation). If you are using UIView animations, it could still be useful since the UIView animations use CAAnimations internally.

I would suggest that you use a CABasicAnimation (or another subclass of CAAnimation if you want more control) to do your animations.
Add the first animation to the view's CALayer when the first event comes in.
When the second event (or any subsequent) triggers, cancel all previous animations (removeAllAnimations or removeAnimationForKey:) and then add the second animation.
In this way your first animation is cancelled immediately, and your second animation will also start immediately.

Assuming that the first and the second animation both animate the same property on the same layer, you probably want the second animation to start from the current value (where the first animation stopped). To achieve this you can set the fromValue of the second animation to the value of the presentation layer (the value as displayed on the screen).
Something like:
CABasicAnimation * secondAnimation = [CABasicAnimation animationForKeyPath:@"transform"];
....set your other animation properties....
secondAnimation.fromValue = [NSValue valueWithCATranform3D:
((CALayer*)layerToAnimate.presentationLayer).transform];
[layerToAnimate addAnimation:secondAnimation forKey:@"myAnimation"];

UIViewAnimationOptionBeginFromCurrentState is the UIView equivalent for this.

If your animations always use the same key, then your old animation will be overwritten by the new animation (and thus the first animation gets cancelled automatically).

Hope it helps ;)
 
Cheers,

Christian
--
You received this message because you are subscribed to the Google Groups "CocoaHeads NL" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cocoaheadsnl...@googlegroups.com.
To post to this group, send email to cocoah...@googlegroups.com.
Visit this group at http://groups.google.com/group/cocoaheadsnl.
For more options, visit https://groups.google.com/groups/opt_out.

Axel Roest

unread,
Jan 22, 2014, 1:57:42 PM1/22/14
to cocoah...@googlegroups.com
Nice explanation Christian, thanks!


Sent from the road, so contents might be bumpy.

Sander de Boer

unread,
Jan 22, 2014, 5:32:58 PM1/22/14
to cocoah...@googlegroups.com
You can also use spritekit SKActions, but christians answer (just slightly different ) will apply too. Difficult to say without context.

Gijs

unread,
Jan 22, 2014, 6:45:37 PM1/22/14
to cocoah...@googlegroups.com
Hee Christian,
I couldn't have said it better, since I learned all the tricks from you! ;)

And to complete your explanation I just couldn't help but add a link to Apples description of this:
"Stopping an Explicit Animation While It Is Running"


Peter, If you download the pdf of this doc, and do a search for "presentation", you will jump to most of the interesting parts in the document regarding your question.
(Because it is about the presentation layer)

hope this helps!

Gijs

Jeroen Leenarts

unread,
Jan 23, 2014, 1:52:30 AM1/23/14
to cocoah...@googlegroups.com
I would actually not rely on doing your own animations, too fiddly and error prone.
Either use CALayer implicit animations or UIView based animations with the proper keys.
When doing UIView animations, UIKit should just queue the animations after each other. Just add your new animation and UIKit should render a nice transition from the initial animation to your new state. The key UIViewAnimationOptionBeginFromCurrentState option key  allows you to change this behaviour.
With UIKit I hardly run into situations that I really have use CAAnimations.
Also, if iOS 7 only is an option, UI Dynamics is truly awesome... (Although you can quickly run into frame rate problems with older devices.)
Reply all
Reply to author
Forward
0 new messages