Categories
Presentation Programming

#iOSDevUK: UIKit Dynamics

My notes from Simon‘s excellent talk at iOSDevUK [his slides].

simulation of the world exploding

Physics for UIKit.

“Visual layers and realistic motion impart vitality and heighten users’ delight and understanding”

Buttons used to look like buttons, now they don’t. Don’t have visual cues. Compensate using Dynamics to add some motion.

Not for flappy birds. Has tried it, can get most of the way there. But there are fundamental missing parts. This is the road to disaster. Not what it is designed for, there are better frameworks out there.

How it works:

  • Physics without having to understand all the hard stuff.
  • UIDynamicItem (e.g. a view): a thing you can animate around the screen.
  • UIDynamicBehavior (e.g. gravity): things that can happen to those things.
  • UIDynamicAnimator: ties those together, responsible for running those animations.

UIDynamicItem

  • Just a protocol.
  • Implemented by UIView.
  • Defines 3 animatable properties:
    • bounds
    • center
    • transform

UIDynamicAnimator

  • Responsible for applying behaviours to items.
  • Defines a reference view, whose coordinate system is used for animations.
  • Manages animations.
  • Pauses when animations end.
  • Use in a ViewController. Should be a property in a VC. Use VC’s own view as the reference view.

UIDynamicBehavior

  • Models a real world physical behaviour.

Built in behaviours:

  • Attachement behaviour:
    • Connects two views together.
    • Move one view and another moves with it.
    • Can use an anchor point rather than a second view.
    • Can be springy (see damping, frequency properties).
  • Collision
  • Gravity
    • Models gravity.
    • Add to an item and it falls downwards.
    • You can optionally configure magnitude and angle.
  • Push
    • Models a force being applied to an item.
    • Instantaneous (a kick) or continuous (a rocket engine).
  • Snap
    • Snap a view back into place.
    • Imagine held in place by four springs.
    • Set damping to control how bouncy it is.
  • DynamicItem
    • Just another dynamic behaviour.
    • Used to tell the animator about the state of dynamic items.
    • Like a meta behaviour.
    • Add additional properties:
      • Density
      • Elasticity
      • Linear velocity
      • Etc…

Gotcha

Animators pause when nothing left to animate. what happens when it goes outside bounds?

Actually view continues accelerating as it falls down towards the centre of the earth.

This is bad because UIViews are flammable. If carried on unchecked, the earth explodes. Which is bad because kittens will die.

Stopping the animation:

  • All dynamic behaviours have an action block.
  • Gets executed for every frame
  • Determine if out of view, if so remove from frame.

Basics. This is it, no hidden complicated stuff.

Tweetbot uses excessively extensively.

Summary:

  • New in iOS 7
  • Give real physical properties with little code
    • Create animator
    • Create behaviour
    • Associate

Do:

  • Delight and inform

Don’t:

  • Try and use with auto layout.
  • Try and recreate angry birds.