Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to UIKit Dynamics (iOSCon 2014)

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Introduction to UIKit Dynamics (iOSCon 2014)

My talk on UIKit Dynamics given at iOSCon 2014 in London

Simon Whitaker

May 16, 2014

Transcript

49 slides
  1. What is UIKit Dynamics? • Added in iOS 7 •

    Adds a physics engine to UIKit
  2. “The UI helps users understand and interact with the content,

    but never competes with it.” iOS Human Interface Guidelines: Designing for iOS 7
  3. “Visual layers and realistic motion impart vitality and heighten users’

    delight and understanding.” iOS Human Interface Guidelines: Designing for iOS 7
  4. UIDynamicItem • Just a protocol • UIView implements it •

    Defines three animatable properties: • bounds • center • transform
  5. UIDynamicAnimator • Responsible for applying behaviours to items • Defines

    a reference view, whose coordinate system is used for animations • Manages animations • Pauses when animations end
  6. UIDynamicBehavior • Models a real-world physical behaviour • UIKit provides

    a number of concrete subclasses • You can create your own
  7. UIGravityBehavior • Models gravity • Add to an item and

    it falls downwards • You can optionally configure magnitude and angle
  8. Units • Earth gravity: 9.8 m/s² • UIKit gravity: 1000

    pt/s² • Adjust with magnitude property
  9. Stopping the animation • UIGravityBehavior will continue to animate endlessly,

    sending your view further and further down • Use the gravity behaviour’s action block • Called once every animation cycle
  10. UICollisionBehavior • Allows you to specify the way dynamic items

    react to the things around them • Collide with other dynamic items and/or boundaries
  11. UIAttachmentBehavior • Connects one view to another view • Move

    one view and the other one moves too, as though attached by an invisible piece of string • Can use an anchor point (CGPoint) instead of second view • Can attach to a specific point within a view
  12. UIOffset offset = UIOffsetMake(40, 40); UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc]

    initWithItem:view1 offsetFromCenter:offset attachedToItem:view2 offsetFromCenter:UIOffsetZero]]; [animator addBehavior:attachment];
  13. UISnapBehavior • Snaps a view to a given point with

    a springy animation • Imagine the view is held in its final position by four springs • Set damping to control how bouncy it is
  14. CGPoint endPoint = CGPointMake(120, 130); UISnapBehavior *snap = [[UISnapBehavior alloc]

    initWithItem:view1 snapToPoint:endPoint]]; [animator addBehavior:snap];
  15. UIDynamicItemBehavior • Just another UIDynamicBehavior • Use to tell the

    animator about the state of dynamic items • density • elasticity • addLinearVelocity:forItem: • etc…
  16. UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.bannerView]]; CGFloat boundaryY = self.bannerView.bounds.size.height;

    [collision addBoundaryWithIdentifier:@"floor" fromPoint:CGPointMake(0, boundaryY) toPoint:CGPointMake(self.view.bounds.size.width, boundaryY) ]; [self.animator addBehavior:collision];
  17. UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.bannerView]]; CGFloat boundaryY = self.bannerView.bounds.size.height;

    [collision addBoundaryWithIdentifier:@"floor" fromPoint:CGPointMake(0, boundaryY) toPoint:CGPointMake(self.view.bounds.size.width, boundaryY) ]; [self.animator addBehavior:collision];
  18. UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.bannerView]]; CGFloat boundaryY = self.bannerView.bounds.size.height;

    [collision addBoundaryWithIdentifier:@"floor" fromPoint:CGPointMake(0, boundaryY) toPoint:CGPointMake(self.view.bounds.size.width, boundaryY) ]; [self.animator addBehavior:collision];
  19. UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.bannerView]]; [self.animator addBehavior:gravity]; ! UIDynamicItemBehavior

    *itemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.bannerView]]; itemBehavior.elasticity = 0.4; [self.animator addBehavior:itemBehavior];
  20. UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.bannerView]]; [self.animator addBehavior:gravity]; ! UIDynamicItemBehavior

    *itemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.bannerView]]; itemBehavior.elasticity = 0.4; [self.animator addBehavior:itemBehavior];
  21. Highlights UIDynamicItemBehavior • Get the velocity from the pan gesture

    recognizer using 
 -velocityInView: • Set the same velocity on the dynamic item behavior using 
 -addLinearVelocity:forItem:
  22. UIKit Dynamics Summary • New in iOS 7 • Give

    UIViews real-world physical behaviours with very little code: • Create animator, associate with a reference view • Create behaviours, associate with dynamic items • Add the behaviours to the animator
  23. UIKit Dynamics Summary • DO: delight and inform users of

    your UI • DON’T: try to use with Auto Layout • DON’T: rewrite Flappy Birds in UIKit

More Decks by Simon Whitaker

Other Decks in Programming