cs193p – Lecture #12 Dynamic Animation

von MichaelMaggs Edit by Richard Bartz (Eigenes Werk) [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons

Please note, this blog entry is from a previous course. You might want to check out the current one.

The twelfth lecture addresses a different approach to animation, creating sets of behaviors defining how items should interact with each other and the environment.

First we create an animator inside a reference view:

var animator = UIDynamicAnimator(referenceView: UIView)

Then we create behaviors, which we add to the animator:

let gravity = UIGravityBehavior() 
let collider = UICollisionBehavior() 

Then we add items to the behaviors:

let item1: UIDynamicItem = ... 
let item2: UIDynamicItem = ... 

Each items needs to be a dynamic item:

protocol UIDynamicItem {
    varbounds: CGRect { get }
    varcenter: CGPoint { get set }
    vartransform: CGAffineTransform { get set }

If an item changes while the animator is running, it needs to be informed (updateItemUsingCurrentState).


The gravity behavior is defined by its direction and magnitude.
The attachment behavior binds an item to an anchor or another item.
The collision behavior defines how an item should react when it comes in contact with other items or boundaries. Collisions can be detected using delegate methods.
The snap behavior lets items snap to certain points.
The push behavior provides an acceleration to an item.

In addition meta behavior allows to define the elasticity, friction and rotation of items.

Behaviors can be adjusted and/or monitored using their action handlers.

When an animator stops – because the items have reach an equilibrium – or when it starts again, various delegate methods get fired (dynamicAnimatorDidPause, dynamicAnimatorWillResume).

Special care needs to be take to avoid memory cycles!

The rest of the lecture is a details demo of the just learned …

The lecture and its slides are available via iTunes named “12. Dynamic Animation”. The code for Dropit is available on GitHub and at Stanford.


Leave a Reply

Your email address will not be published.