SlideShare ist ein Scribd-Unternehmen logo
1 von 34
Downloaden Sie, um offline zu lesen
UIKit Dynamics
Craig VanderZwaag

craig@bluehulastudios.com
Tuesday, October 15, 13

@bHCraigV
ADD DEPTH
AND GRAVITY
AND WEIGHT
AND DELIGHT!

Tuesday, October 15, 13
UIKIT DYNAMICS
• New

in iOS7 SDK

• Brings

real-world physics and interaction
behaviors to your apps

• 2-Dimensional

interaction

Tuesday, October 15, 13

animations and
DEMO
Tuesday, October 15, 13
HOW?
• Integrated

physics engine and animator

• PhysicsKit-

Shared with the new Sprite
Kit framework

• Based

Tuesday, October 15, 13

on Box 2D http://box2d.org
CONCEPTS
• Dynamic

Items

• Dynamic

Behaviors

• Dynamic Animators

Tuesday, October 15, 13
DYNAMIC ITEMS
• Objects

that can be animated

• Minimal

set of geometry properties

• Don’t

necessarily have to be views or
even on screen

Tuesday, October 15, 13
DYNAMIC BEHAVIORS
• Encapsulate
• Associated

a set of “physical” attributes and behaviors

to dynamic items to impart them with

behaviors
• Gravity, Collision, Friction
• Resistance, Velocity, Attachment
• Can

Be composed in a hierarchy

Tuesday, October 15, 13
DYNAMIC ANIMATOR
• Coordinates

a view

the dynamic items and behaviors in

• Updates

the “scene” with each step of the
animation

• Holds

all the behaviors that describe how
dynamic items react

Tuesday, October 15, 13
CODE
 TIME

Tuesday, October 15, 13
STEP ONE:
CREATE A DYNAMIC ITEM
@protocol UIDynamicItem NSObject
@property (nonatomic, readwrite) CGPoint center;
@property (nonatomic, readonly) CGRect bounds;
@property (nonatomic, readwrite) CGAffineTransform transform;
@end

NS_CLASS_AVAILABLE_IOS(2_0) @interface UIView :
UIResponderNSCoding, UIAppearance, UIAppearanceContainer,
UIDynamicItem {

Tuesday, October 15, 13
STEP ONE:
CREATE A DYNAMIC ITEM
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = {CGPointZero, {100.0, 100.0}};
self.button = [[UIButton alloc] initWithFrame:frame];
self.button.backgroundColor = [UIColor purpleColor];
[self.view addSubview:self.button];
...
}

Tuesday, October 15, 13
STEP TWO:
CREATE A DYNAMIC ANIMATOR
NS_CLASS_AVAILABLE_IOS(7_0) @interface UIDynamicAnimator: NSObject
- (instancetype)initWithReferenceView:(UIView*)view;
- (void)addBehavior:(UIDynamicBehavior *)behavior;
- (void)removeBehavior:(UIDynamicBehavior *)behavior;
- (void)removeAllBehaviors;
@property (nonatomic, readonly) UIView* referenceView;
@property (nonatomic, readonly, copy) NSArray* behaviors;

Tuesday, October 15, 13
STEP TWO:
CREATE A DYNAMIC ANIMATOR
@implementation ViewController
- (void)viewDidLoad
{
...
self.animator = [[UIDynamicAnimator alloc]
initWithReferenceView:self.view];
...
}

Tuesday, October 15, 13
STEP THREE:
ADD BEHAVIORS
NS_CLASS_AVAILABLE_IOS(7_0) @interface UIDynamicBehavior : NSObject
- (void)addChildBehavior:(UIDynamicBehavior *)behavior;
- (void)removeChildBehavior:(UIDynamicBehavior *)behavior;
@property (nonatomic, readonly, copy) NSArray* childBehaviors;
// When running, the dynamic animator calls the action block on
every animation step.
@property (nonatomic,copy) void (^action)(void);
- (void)willMoveToAnimator:(UIDynamicAnimator *)dynamicAnimator; //
nil when being removed from an animator
@property (nonatomic, readonly) UIDynamicAnimator *dynamicAnimator;
@end

Tuesday, October 15, 13
STEP THREE:
ADD BEHAVIORS- GRAVITY
NS_CLASS_AVAILABLE_IOS(7_0) @interface UIGravityBehavior :
UIDynamicBehavior
- (instancetype)initWithItems:(NSArray *)items;
- (void)addItem:(id UIDynamicItem)item;
- (void)removeItem:(id UIDynamicItem)item;
@property (nonatomic, readonly, copy) NSArray* items;
// The default value for the gravity vector is (0.0, 1.0)
// The acceleration for a dynamic item subject to a (0.0, 1.0) gravity vector is
downwards at 1000 points per second².

@property (readwrite, nonatomic) CGVector gravityDirection;

@property (readwrite, nonatomic) CGFloat angle;
@property (readwrite, nonatomic) CGFloat magnitude;
- (void)setAngle:(CGFloat)angle magnitude:(CGFloat)magnitude;
@end

Tuesday, October 15, 13
STEP THREE:
ADD BEHAVIORS- GRAVITY
- (void)viewDidLoad
{
[super viewDidLoad];
...
UIGravityBehavior* gravityBehavior = [[UIGravityBehavior alloc]
initWithItems:@[self.button]];
[self.animator addBehavior:gravityBehavior];
...
}

Tuesday, October 15, 13
TO
 XCODE
 WE
 GO
https://bitbucket.org/craigvz/dynamicssampler

Tuesday, October 15, 13
Tuesday, October 15, 13
WHAT HAPPENED?

Tuesday, October 15, 13
•We

fell through the floor

•Button

needs to collide with the edge of
our view to stay on the screen

Tuesday, October 15, 13
STEP FOUR:
ADD BEHAVIORS- COLLISION
NS_CLASS_AVAILABLE_IOS(7_0) @interface UICollisionBehavior :
UIDynamicBehavior
- (instancetype)initWithItems:(NSArray *)items;
- (void)addItem:(id UIDynamicItem)item;
- (void)removeItem:(id UIDynamicItem)item;
@property (nonatomic, readonly, copy) NSArray* items;
@property (nonatomic, readwrite) UICollisionBehaviorMode collisionMode;
@property (nonatomic, readwrite) BOOL
translatesReferenceBoundsIntoBoundary;
- (void)setTranslatesReferenceBoundsIntoBoundaryWithInsets:
(UIEdgeInsets)insets;
...
@end

Tuesday, October 15, 13
STEP FOUR:
ADD BEHAVIORS- COLLISION
- (void)viewDidLoad
{
[super viewDidLoad];
...
UICollisionBehavior* collisionBehavior = [[UICollisionBehavior
alloc] initWithItems:@[self.button]];
collisionBehavior.collisionMode =
UICollisionBehaviorModeBoundaries;
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
[self.animator addBehavior:collisionBehavior];
...
}

Tuesday, October 15, 13
STEP FIVE:
ADD BEHAVIORS- PUSH
NS_CLASS_AVAILABLE_IOS(7_0) @interface UIPushBehavior : UIDynamicBehavior
- (instancetype)initWithItems:(NSArray *)items mode:(UIPushBehaviorMode)mode;
- (void)addItem:(id UIDynamicItem)item;
- (void)removeItem:(id UIDynamicItem)item;
@property (nonatomic, readonly, copy) NSArray* items;
- (UIOffset)targetOffsetFromCenterForItem:(id UIDynamicItem)item;
- (void)setTargetOffsetFromCenter:(UIOffset)o forItem:(id UIDynamicItem)item;
@property (nonatomic, readonly) UIPushBehaviorMode mode;
@property (nonatomic, readwrite) BOOL active;
@property (readwrite, nonatomic) CGFloat angle;
// A continuous force vector with a magnitude of 1.0, applied to a 100 point x 100 point
view whose density value is 1.0, results in view acceleration of 100 points per s^2
@property (readwrite, nonatomic) CGFloat magnitude;
@property (readwrite, nonatomic) CGVector pushDirection;
- (void)setAngle:(CGFloat)angle magnitude:(CGFloat)magnitude;
@end

Tuesday, October 15, 13
STEP FIVE:
ADD BEHAVIORS- PUSH
- (void)viewDidLoad
{
...
self.pushBehavior = [[UIPushBehavior alloc]
initWithItems:@[self.button]
mode:UIPushBehaviorModeInstantaneous];
CGFloat magnitude = 20.0;
[self.pushBehavior setAngle:-M_PI_2 magnitude:magnitude];
self.pushBehavior.active = NO;
[self.animator addBehavior:self.pushBehavior];
}

...

Tuesday, October 15, 13
STEP SIX:
TWEAK ITEM PROPERTIES
NS_CLASS_AVAILABLE_IOS(7_0) @interface UIDynamicItemBehavior : UIDynamicBehavior
...
@property (readwrite, nonatomic) CGFloat elasticity; // Usually between 0
(inelastic) and 1 (collide elastically)
@property (readwrite, nonatomic) CGFloat friction; // 0 being no friction between
objects slide along each other
@property (readwrite, nonatomic) CGFloat density; // 1 by default
@property (readwrite, nonatomic) CGFloat resistance; // 0: no velocity damping
@property (readwrite, nonatomic) CGFloat angularResistance; // 0: no angular
velocity damping
@property (readwrite, nonatomic) BOOL allowsRotation; // force an item to never
rotate
...
@end

Tuesday, October 15, 13
STEP SIX:
TWEAK ITEM PROPERTIES
NS_CLASS_AVAILABLE_IOS(7_0) @interface UIDynamicItemBehavior : UIDynamicBehavior
...
// The linear velocity, expressed in points per second, that you want to add to
the specified dynamic item
// If called before being associated to an animator, the behavior will accumulate
values until being associated to an animator
- (void)addLinearVelocity:(CGPoint)velocity forItem:(id UIDynamicItem)item;
- (CGPoint)linearVelocityForItem:(id UIDynamicItem)item;
// The angular velocity, expressed in radians per second, that you want to add to
the specified dynamic item
// If called before being associated to an animator, the behavior will accumulate
values until being associated to an animator
- (void)addAngularVelocity:(CGFloat)velocity forItem:(id UIDynamicItem)item;
- (CGFloat)angularVelocityForItem:(id UIDynamicItem)item;
@end

Tuesday, October 15, 13
STEP SIX:
TWEAK ITEM PROPERTIES
- (void)viewDidLoad
{
[super viewDidLoad];
...
UIDynamicItemBehavior* dynamicItemBehavior =
[[UIDynamicItemBehavior alloc] initWithItems:@[self.button]];
dynamicItemBehavior.elasticity = 0.5;
dynamicItemBehavior.resistance = 1.0;
...
}

Tuesday, October 15, 13
UNITS

Tuesday, October 15, 13
UNITS
• Many

properties have their own unit of measure

• UIGravityBehavior’s
• 1.0

= 1000 points / second2

• UIPushBehavior
• 1.0

magnitude:

magnitude:

= 100 points / second2 for 100 point x 100 point item
with density = 1.0 (UIKit Newton)

Tuesday, October 15, 13

Weitere ähnliche Inhalte

Kürzlich hochgeladen

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Kürzlich hochgeladen (20)

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 

Empfohlen

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Empfohlen (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

UIKit Dynamics

  • 2. ADD DEPTH AND GRAVITY AND WEIGHT AND DELIGHT! Tuesday, October 15, 13
  • 3. UIKIT DYNAMICS • New in iOS7 SDK • Brings real-world physics and interaction behaviors to your apps • 2-Dimensional interaction Tuesday, October 15, 13 animations and
  • 5. HOW? • Integrated physics engine and animator • PhysicsKit- Shared with the new Sprite Kit framework • Based Tuesday, October 15, 13 on Box 2D http://box2d.org
  • 6. CONCEPTS • Dynamic Items • Dynamic Behaviors • Dynamic Animators Tuesday, October 15, 13
  • 7. DYNAMIC ITEMS • Objects that can be animated • Minimal set of geometry properties • Don’t necessarily have to be views or even on screen Tuesday, October 15, 13
  • 8. DYNAMIC BEHAVIORS • Encapsulate • Associated a set of “physical” attributes and behaviors to dynamic items to impart them with behaviors • Gravity, Collision, Friction • Resistance, Velocity, Attachment • Can Be composed in a hierarchy Tuesday, October 15, 13
  • 9. DYNAMIC ANIMATOR • Coordinates a view the dynamic items and behaviors in • Updates the “scene” with each step of the animation • Holds all the behaviors that describe how dynamic items react Tuesday, October 15, 13
  • 10. CODE
  • 12. STEP ONE: CREATE A DYNAMIC ITEM @protocol UIDynamicItem NSObject @property (nonatomic, readwrite) CGPoint center; @property (nonatomic, readonly) CGRect bounds; @property (nonatomic, readwrite) CGAffineTransform transform; @end NS_CLASS_AVAILABLE_IOS(2_0) @interface UIView : UIResponderNSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem { Tuesday, October 15, 13
  • 13. STEP ONE: CREATE A DYNAMIC ITEM - (void)viewDidLoad { [super viewDidLoad]; CGRect frame = {CGPointZero, {100.0, 100.0}}; self.button = [[UIButton alloc] initWithFrame:frame]; self.button.backgroundColor = [UIColor purpleColor]; [self.view addSubview:self.button]; ... } Tuesday, October 15, 13
  • 14. STEP TWO: CREATE A DYNAMIC ANIMATOR NS_CLASS_AVAILABLE_IOS(7_0) @interface UIDynamicAnimator: NSObject - (instancetype)initWithReferenceView:(UIView*)view; - (void)addBehavior:(UIDynamicBehavior *)behavior; - (void)removeBehavior:(UIDynamicBehavior *)behavior; - (void)removeAllBehaviors; @property (nonatomic, readonly) UIView* referenceView; @property (nonatomic, readonly, copy) NSArray* behaviors; Tuesday, October 15, 13
  • 15. STEP TWO: CREATE A DYNAMIC ANIMATOR @implementation ViewController - (void)viewDidLoad { ... self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; ... } Tuesday, October 15, 13
  • 16. STEP THREE: ADD BEHAVIORS NS_CLASS_AVAILABLE_IOS(7_0) @interface UIDynamicBehavior : NSObject - (void)addChildBehavior:(UIDynamicBehavior *)behavior; - (void)removeChildBehavior:(UIDynamicBehavior *)behavior; @property (nonatomic, readonly, copy) NSArray* childBehaviors; // When running, the dynamic animator calls the action block on every animation step. @property (nonatomic,copy) void (^action)(void); - (void)willMoveToAnimator:(UIDynamicAnimator *)dynamicAnimator; // nil when being removed from an animator @property (nonatomic, readonly) UIDynamicAnimator *dynamicAnimator; @end Tuesday, October 15, 13
  • 17. STEP THREE: ADD BEHAVIORS- GRAVITY NS_CLASS_AVAILABLE_IOS(7_0) @interface UIGravityBehavior : UIDynamicBehavior - (instancetype)initWithItems:(NSArray *)items; - (void)addItem:(id UIDynamicItem)item; - (void)removeItem:(id UIDynamicItem)item; @property (nonatomic, readonly, copy) NSArray* items; // The default value for the gravity vector is (0.0, 1.0) // The acceleration for a dynamic item subject to a (0.0, 1.0) gravity vector is downwards at 1000 points per second². @property (readwrite, nonatomic) CGVector gravityDirection; @property (readwrite, nonatomic) CGFloat angle; @property (readwrite, nonatomic) CGFloat magnitude; - (void)setAngle:(CGFloat)angle magnitude:(CGFloat)magnitude; @end Tuesday, October 15, 13
  • 18. STEP THREE: ADD BEHAVIORS- GRAVITY - (void)viewDidLoad { [super viewDidLoad]; ... UIGravityBehavior* gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[self.button]]; [self.animator addBehavior:gravityBehavior]; ... } Tuesday, October 15, 13
  • 19. TO
  • 21.  WE
  • 25. •We fell through the floor •Button needs to collide with the edge of our view to stay on the screen Tuesday, October 15, 13
  • 26. STEP FOUR: ADD BEHAVIORS- COLLISION NS_CLASS_AVAILABLE_IOS(7_0) @interface UICollisionBehavior : UIDynamicBehavior - (instancetype)initWithItems:(NSArray *)items; - (void)addItem:(id UIDynamicItem)item; - (void)removeItem:(id UIDynamicItem)item; @property (nonatomic, readonly, copy) NSArray* items; @property (nonatomic, readwrite) UICollisionBehaviorMode collisionMode; @property (nonatomic, readwrite) BOOL translatesReferenceBoundsIntoBoundary; - (void)setTranslatesReferenceBoundsIntoBoundaryWithInsets: (UIEdgeInsets)insets; ... @end Tuesday, October 15, 13
  • 27. STEP FOUR: ADD BEHAVIORS- COLLISION - (void)viewDidLoad { [super viewDidLoad]; ... UICollisionBehavior* collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.button]]; collisionBehavior.collisionMode = UICollisionBehaviorModeBoundaries; collisionBehavior.translatesReferenceBoundsIntoBoundary = YES; [self.animator addBehavior:collisionBehavior]; ... } Tuesday, October 15, 13
  • 28. STEP FIVE: ADD BEHAVIORS- PUSH NS_CLASS_AVAILABLE_IOS(7_0) @interface UIPushBehavior : UIDynamicBehavior - (instancetype)initWithItems:(NSArray *)items mode:(UIPushBehaviorMode)mode; - (void)addItem:(id UIDynamicItem)item; - (void)removeItem:(id UIDynamicItem)item; @property (nonatomic, readonly, copy) NSArray* items; - (UIOffset)targetOffsetFromCenterForItem:(id UIDynamicItem)item; - (void)setTargetOffsetFromCenter:(UIOffset)o forItem:(id UIDynamicItem)item; @property (nonatomic, readonly) UIPushBehaviorMode mode; @property (nonatomic, readwrite) BOOL active; @property (readwrite, nonatomic) CGFloat angle; // A continuous force vector with a magnitude of 1.0, applied to a 100 point x 100 point view whose density value is 1.0, results in view acceleration of 100 points per s^2 @property (readwrite, nonatomic) CGFloat magnitude; @property (readwrite, nonatomic) CGVector pushDirection; - (void)setAngle:(CGFloat)angle magnitude:(CGFloat)magnitude; @end Tuesday, October 15, 13
  • 29. STEP FIVE: ADD BEHAVIORS- PUSH - (void)viewDidLoad { ... self.pushBehavior = [[UIPushBehavior alloc] initWithItems:@[self.button] mode:UIPushBehaviorModeInstantaneous]; CGFloat magnitude = 20.0; [self.pushBehavior setAngle:-M_PI_2 magnitude:magnitude]; self.pushBehavior.active = NO; [self.animator addBehavior:self.pushBehavior]; } ... Tuesday, October 15, 13
  • 30. STEP SIX: TWEAK ITEM PROPERTIES NS_CLASS_AVAILABLE_IOS(7_0) @interface UIDynamicItemBehavior : UIDynamicBehavior ... @property (readwrite, nonatomic) CGFloat elasticity; // Usually between 0 (inelastic) and 1 (collide elastically) @property (readwrite, nonatomic) CGFloat friction; // 0 being no friction between objects slide along each other @property (readwrite, nonatomic) CGFloat density; // 1 by default @property (readwrite, nonatomic) CGFloat resistance; // 0: no velocity damping @property (readwrite, nonatomic) CGFloat angularResistance; // 0: no angular velocity damping @property (readwrite, nonatomic) BOOL allowsRotation; // force an item to never rotate ... @end Tuesday, October 15, 13
  • 31. STEP SIX: TWEAK ITEM PROPERTIES NS_CLASS_AVAILABLE_IOS(7_0) @interface UIDynamicItemBehavior : UIDynamicBehavior ... // The linear velocity, expressed in points per second, that you want to add to the specified dynamic item // If called before being associated to an animator, the behavior will accumulate values until being associated to an animator - (void)addLinearVelocity:(CGPoint)velocity forItem:(id UIDynamicItem)item; - (CGPoint)linearVelocityForItem:(id UIDynamicItem)item; // The angular velocity, expressed in radians per second, that you want to add to the specified dynamic item // If called before being associated to an animator, the behavior will accumulate values until being associated to an animator - (void)addAngularVelocity:(CGFloat)velocity forItem:(id UIDynamicItem)item; - (CGFloat)angularVelocityForItem:(id UIDynamicItem)item; @end Tuesday, October 15, 13
  • 32. STEP SIX: TWEAK ITEM PROPERTIES - (void)viewDidLoad { [super viewDidLoad]; ... UIDynamicItemBehavior* dynamicItemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.button]]; dynamicItemBehavior.elasticity = 0.5; dynamicItemBehavior.resistance = 1.0; ... } Tuesday, October 15, 13
  • 34. UNITS • Many properties have their own unit of measure • UIGravityBehavior’s • 1.0 = 1000 points / second2 • UIPushBehavior • 1.0 magnitude: magnitude: = 100 points / second2 for 100 point x 100 point item with density = 1.0 (UIKit Newton) Tuesday, October 15, 13
  • 35. DELEGATES @protocol UIDynamicAnimatorDelegate NSObject @optional - (void)dynamicAnimatorWillResume:(UIDynamicAnimator*)animator; - (void)dynamicAnimatorDidPause:(UIDynamicAnimator*)animator; @end Tuesday, October 15, 13
  • 36. DELEGATES @protocol UICollisionBehaviorDelegate NSObject @optional - (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem: (id UIDynamicItem)item1 withItem:(id UIDynamicItem)item2 atPoint: (CGPoint)p; - (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem: (id UIDynamicItem)item1 withItem:(id UIDynamicItem)item2; // The identifier of a boundary created with translatesReferenceBoundsIntoBoundary or setTranslatesReferenceBoundsIntoBoundaryWithInsets is nil - (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem: (id UIDynamicItem)item withBoundaryIdentifier:(id NSCopying)identifier atPoint:(CGPoint)p; - (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem: (id UIDynamicItem)item withBoundaryIdentifier:(id NSCopying)identifier; @end Tuesday, October 15, 13
  • 38. THE