uikit dynamics-360idev2014

23
Making Your Apps Dynamic An Introduction to UIKit Dynamics Andria Jensen Founder, Logical Zen @andriajensen [email protected] Demo Project: bit.ly/360-dynamics

Upload: andriajensen

Post on 15-Jan-2015

385 views

Category:

Technology


2 download

DESCRIPTION

Making Your Apps Dynamic - Intro to UIKit Dynamics 360iDev Denver 2014

TRANSCRIPT

Page 1: UiKit Dynamics-360idev2014

Making Your Apps Dynamic

An Introduction to UIKit Dynamics

Andria Jensen Founder, Logical Zen

@andriajensen [email protected]

Demo Project: bit.ly/360-dynamics

Page 2: UiKit Dynamics-360idev2014

What does it mean?• Realistic effects and animations

• Using real-world ideas and behaviors to make your app feel more natural

• Powered by a 2D physics engine

Page 3: UiKit Dynamics-360idev2014

The Basics• UIDynamicAnimator

• UIDynamicBehavior

• UIDynamicItem

Page 4: UiKit Dynamics-360idev2014

UIDynamicAnimator• The link between you and the physics engine

• You must create and retain an animator for use

• Reference view to house the animations

self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

Page 5: UiKit Dynamics-360idev2014

UIDynamicBehavior• Definition of how items interact

• An animator may have any number of behaviors

• Gravity, Collision, Push, Attachment, Snap

• …. or anything

gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[view]];

Page 6: UiKit Dynamics-360idev2014

UIDynamicItem • The item that you’d like to use dynamic

behavior(s)

• Anything adopting the UIDynamicItem protocol

• UIView and UICollectionViewLayoutAttributes

Page 7: UiKit Dynamics-360idev2014

UIDynamicAnimator

UIDynamicItem (UIView)

UIDynamicBehavior (Gravity)

UIDynamicBehavior (Collision)

UIDynamicItem (UIView)

UIDynamicItem (UIView)

UIDynamicItem (UIView)

Page 8: UiKit Dynamics-360idev2014

UISnapBehavior• Snaps a view to a specified point

• Can adjust damping value (0 to 1, 0.5 is default)

• Adding the behavior triggers a single snap action

snapBehavior = [[UISnapBehavior alloc] initWithItem:self.dynamicView snapToPoint:point]; !snapBehavior.damping = 0.5; [self.animator addBehavior:snapBehavior];

Page 9: UiKit Dynamics-360idev2014

DEMO

Page 10: UiKit Dynamics-360idev2014

UIGravityBehavior• Standard UIKit gravity: 1 = 1000 points/second2

• gravityDirection: default is 0,1 for a standard downward force

• (angle & magnitude) OR gravityDirection

• Only one allowed per animator

gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[view]]; gravityBehavior.gravityDirection = CGVectorMake(0.0, 1.0); [self.animator addBehavior:gravityBehavior];

Page 11: UiKit Dynamics-360idev2014

UIPushBehavior• A continuous or instantaneous force

• Similar to the gravity behavior, but takes view size into account

• Adjust (angle & magnitude) OR pushDirection

• UIKit Newton measurement 1N = Force to move a 100x100 view at 100 pixels/second2

• Use the active property to turn a push behavior on/off

Page 12: UiKit Dynamics-360idev2014

UIPushBehaviorpushBehavior = [[UIPushBehavior alloc] initWithItems:@[view] mode:UIPushBehaviorModeContinuous]; pushBehavior.pushDirection = CGVectorMake(0.5, 0.5); pushBehavior.active = NO; ![self.animator addBehavior:pushBehavior]; !

Page 13: UiKit Dynamics-360idev2014

DEMO

Page 14: UiKit Dynamics-360idev2014

UIAttachmentBehavior• Anchors an item to a given point, or another item

• Can adjust length, frequency, & damping

• Think of attachments like springs between items

• Default attachment point is item’s center

• Use with gestures to make moving views around easy

Page 15: UiKit Dynamics-360idev2014

UIAttachmentBehaviorattachment = [[UIAttachmentBehavior alloc] initWithItem:self.blueView attachedToItem:self.orangeView]; !attachment.length = 100.0; attachment.frequency = 0.1; attachment.damping = 0.2; ![self.animator addBehavior:attachment];

Page 16: UiKit Dynamics-360idev2014

UIAttachmentBehavior- (IBAction) handlePanGesture:(UIPanGestureRecognizer *)gesture { CGPoint touchPoint = [gesture locationInView:self.view]; UIView* draggedView = gesture.view; if (gesture.state == UIGestureRecognizerStateBegan) { // pan started, create an attachment starting at the initial touch point self.panAttachment = [[UIAttachmentBehavior alloc] initWithItem:draggedView attachedToAnchor:touchPoint]; [self.animator addBehavior:self.panAttachment]; } else if (gesture.state == UIGestureRecognizerStateChanged) { // update the anchor point as the pan is moving around self.panAttachment.anchorPoint = touchPoint; } else if (gesture.state == UIGestureRecognizerStateEnded) { // remove the attachment behavior as soon as the pan ends [self.animator removeBehavior:self.panAttachment]; } }

Page 17: UiKit Dynamics-360idev2014

DEMO

Page 18: UiKit Dynamics-360idev2014

UICollisionBehavior• Collisions between boundaries or items

• Use translatesReferenceBoundsIntoBoundary to make your reference view’s frame the boundary

• Use collisionMode to specify whether items collide with boundaries, other items, or both

• Multiple collision behaviors can be added to an animator

• Use UICollisionDelegate to know when collisions occur

Page 19: UiKit Dynamics-360idev2014

UICollisionBehaviorcollisionBehavior = [[UICollisionBehavior alloc] initWithItems:collisionViews]; collisionBehavior.translatesReferenceBoundsIntoBoundary = YES; [collisionBehavior addBoundaryWithIdentifier:identifier fromPoint:origin toPoint:topRightPoint]; ![self.animator addBehavior:collisionBehavior];

Page 20: UiKit Dynamics-360idev2014

UIDynamicItemBehaviorvelocity

friction

elasticity

resistance

density

allowsRotation

Page 21: UiKit Dynamics-360idev2014

DEMO

Page 22: UiKit Dynamics-360idev2014

Composite Behaviors• A combination of several behaviors

• Use a UIDynamicBehavior subclass to easily re-use composite behaviors

• [self addChildBehavior:collisionBehavior]

Page 23: UiKit Dynamics-360idev2014

Questions?

Andria Jensen Founder, Logical Zen

!@andriajensen

[email protected]

Demo Project: bit.ly/360-dynamics