UIKit Dynamics Overview

Yes, now you can have all your UI objects to behave like how objects in a physics environment behave.
You can have physics concepts like Gravity, Collisions, springs etc..  added to the UI Objects.

Apple updated the UIKit framework (which contains almost all the iOS UI Elements) such that any physics behavior could be added to the UIKit Elements/Objects. Hence,  2d physics engine lies just underneath the UIKit to simulate the real world objects.

In order to use UIKit Dynamics, we use "UIDynamicBehavior" to apply different behaviors to objects, which binds to the "UIDynamicItem" protocol, UIView does this already for you, hence any subclass of UIView just works with this. You can even create your own objects binding to the above protocol.

After setting/applying all the physics behaviors to the objects that we needed to have, we can provide them to the "UIDynamicAnimator" - think this as the controller of UIKit Dynamics, which handles/calculates on how each and every object must behave based on their behaviors specified.

There are several behavior classes that we could use to add behavior to our objects:
# UIGravityBehavior (Represents the gravitation attraction between the object and the Earth)
# UIPushBehavior (Represents a simple linear force applied to the objects)
UIAttachmentBehavior (It attaches the dynamic objects either to a static point or to an another object)
UICollisionBehavior (To detect collisions with other objects)
UIDynamicItemBehavior (Defines to a set of objects. It has properties to define common behavior to a group of objects, such as the elasticity of objects when collision occurs between them)

The above are all subclasses of the "UIDynamicBehavior", which defines the behavior of objects.
Each class have properties to configure the objects behavior.

Below is one simple example considering an array of UI objects are being applied a gravitational force.

Example :

    NSArray * objects = "set of UI Objects on which you want to add/apply physics behaviors"
    UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    UIDynamicBehavior *behavior = [[UIDynamicBehavior alloc] init];
    UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:objects];
    gravity.magnitude = 10; // It defines the gravitational force.
    [animator addBehavior:behavior];

Comments

Popular posts from this blog

Free Hand Drawing on Google's Map View

Android from iOS Guy's Perspective

Free Hand Drawing on Maps with MapBox SDK