A change in plan…

I was going to start off with a discussion about JSON, but when I started working on a project it got me really excited. This week lets chat about UIKit Dynamics!

What is it?

UIKit Dynamics

Apple has figured out a way to manage a physics engine in iOS Foundation Like A Boss

like-a-boss1

lets discuss a little of what it does exactly:

It manages gravity (things must fall), collisions (things are gonna crash, sometimes), attachments, springs, snap, force (think like a Jedi) plus they inherit properties allowsRotation, density (affects gravity and collision reactions), elasticity (bounce), and resistance.

For some of these things, you may have had a taste of it already on your lock screen.

swipe Down

swipe Down

So if you swipe lightly, it won’t fall. Swipe harder and it falls with a gentle bounce at the bottom.

That’s UIDynamicAnimator, UIGravityBehavior, and UICollisionBehavior all for you folks!

Same occurs at the bottom with the camera, the lock slide, and the control center.

to see the code which you need to call, first and foremost the UIDynamicAnimator

one way calling the delegate and property


@interface ViewController () <UIDynamicAnimatorDelegate>

@property (strong, nonatomic) UIDynamicAnimator *animator;

another way is to call the class, alloc and init


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

[animator addBehavior:someDynamicBehavior];

UIGravityBehavior works just the way you would imagine:

Kangaroo-Drops-Ball

It will make things fall. They will fall toward the home button, no matter which way the phone orientation is.

to fix this there is a method called setGravityDirection:CGVectorMake(x float, y float) which can “sway” the direction of -whatever- is falling in the direction you want. For a drop down action, x stays at 0.0f and y can be any float between 0.1 and 1 (1 making us fall the fastest).

If x is incremented to 0.1, our object will “fall” to the right, -0.1 and it “falls” to the left.

If y is made negative, it will “fall” up.

It makes me curious if we could tap into the gyroscope and make our “falling” dynamic?

falling

Second, it should be noted that if we don’t have UICollisionBehavior activated, our object will fall right off the phone! (and out of sight) Lets call that.

UICollisionBehavior *collide = [[UICollisionBehavior alloc] init];

collide.translatesReferenceBoundsIntoBoundary = YES;

Why ‘YES’? Because it’s a BOOL.

Seriously, it will make our view boundary the limit for which things to contact when ‘falling’. Without it we will have no limit, and objects will ‘fall’ right off the phone.

giphy

It has been said that UIKit Dynamics and AutoLayout don’t play nicely. Keep them apart until Apple teaches them to share screen real estate (iOS 8?)

Why is this important?

Previous to iOS7, it was not native. We would need an engine imported from somewhere else. For developers, it is easier to make games, or apps that react to your users’ particular movements or touch patters.

props to ComedyCentral, Photobucket, CultOfMac & 3dpop for the pics, iOS Components and Frameworks for some snippets!

Advertisements
Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s