Day 45: Stanford CS193p – Dynamic Animation
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:
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
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
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”
More of what the demo looked like:
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.