Day 45: Stanford CS193p – Dynamic Animation

Day 45: Stanford CS193p – Dynamic Animation

Screen Shot 2015-06-26 at 2.35.44 AM
lecture 12 – Dynamic Animation

“Physics Based”
Applying animation to the user experience is a great use of this.
For lecture, building a game and having fun.

Set up physics relating animatable objects and let them run until they resolve to stasis
Easily possible to set it up so that stasis never occurs, but that could be performance problem

Create a UIDynamicAnimator
Add UIDynamicBehavior to it (gravity, collisions)
Add UIDynamicItem (usually UIViews) to the UIDynamic Behavior
(UIDynamicItem is a protocol which UIView happens to implement)

Create a UIDynamicAnimator:
var animator = UIDynamicAnimator(referenceView: UIView)
if animating views, all views must be in a view hierarchy with referenceView at the top.

Create and add UIDynamicBehavior instances:
let gravity = UIGravityBehavior()
collider = UICollisionBehavior()

Then add UIDynamicItems to a UIDynamicBehavior:

Screen Shot 2015-06-26 at 2.45.18 AM


Screen Shot 2015-06-26 at 2.51.32 AM

var collisionMode: UICollisionBehaviorMode

if .Items, then any items you add to a UICollisionBehavior will bounce off each other
if .Boundaries, then add UIBezierPath boundaries for items to bounce off of…
func addBoundaryWithIdentifier(identifier: NSCopying, forPath: UIBezierPath)
func removeBoundaryWithIdentifier(identifier: NSCopying)
var translatesReferenceBoundsIntoBoundary: Bool


Screen Shot 2015-06-26 at 2.55.07 AM

init(item: UIDynamicItem, snapToPoint: CGPoint)
imagine four springs at four corners around item
you can control the damping of these “four springs” with var damping: CGFloat

var mode: UIPushBehaviorMode
var pushDirection: CGVector
var angle: CGFloat
var magnitude: CGFloat

if push .Instantaneous, what happens?
it just sits there wasting memory!

special “meta” behavior
controls behavior of items asa they affected by other behaviors
any item added to this behavior(with addItem) will be affected by:
var allowsRotation: Bool
var friction: CGFloat
var elasticity: CGFloat
…among others

Can also get info about items with this behavior:
func linearVelocityForItem(UIDynamicItem) -> CGPoint
func addLinearVelocity(CGPoint, forItem: UIDynamicItem)
func angularVelocityForItem(UIDynamicItem) -> CGFloat

multiple UIDynamicItemBehaviors affecting same item(s) is “advanced”

Screen Shot 2015-06-26 at 2.59.35 AM

Screen Shot 2015-06-26 at 3.08.23 AM

Screen Shot 2015-06-26 at 3.25.27 AM

More of what the demo looked like:

Screen Shot 2015-06-26 at 3.25.39 AM

This post is pure note jotting. I will be going over animation in greater detail till I fully get it. I’ve had some experience with animation from building games on my free time, so I understand a chunk of it, but some the finer points are brand new to me. I will for sure be reviewing this over and over again, along with some sources on StackOverFlow.


Published by: Travis Deaton

I studied International Relations at American University, volunteered for various organizations, and have recently returned from a 5 month tour of our National Parks. Currently an EMT/Wildland Fire & SAR student.

Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s