UIKit Dynamics


Hello everyone, today we are going to learn about UIKit Dynamics. I hope you all know what we can do with animations, UIKit Dynamics is somewhat that helps you to apply physics-based animations to your views. Even with less efforts and code you can easily create amazing animations.

Dynamic Animator

Let’s get started with UIDynamicAnimator, it is an object with which you can add physics-related capabilities and animations for the underlying UIDynamicItem.

Some of the methods are very useful and necessary to apply UIDynamics:


It is set of methods that helps any object to being eligible for participation in UIKit Dynamics.

  1. UIDynamicItemBehavior: Its is a class which helps to configure your animations for one or more dynamic items.
  2. UIDynamicItemGroup: It groups together to manipulate group of dynamic items & treat them as a single unit.

Without wasting much time let’s start the fun part….coding!

Here is the listing of UIKit Dynamics variables that we will use in entire project.

2.1 UIGravityBehavior

Use: To apply gravity-like force to object.

UIKit Dynamics Gravity

Here’s the code how you can add Gravity behaviour to squareView

Only setting gravity behaviour is not enough, because by applying this code the squareView falls below the screen. To fix this we have to set some boundaries where squareView will collide and do not exit the screen.
In below code we have given screen boundary to collide with.

You can also make object bounce by adding elasticity to its behaviour.

Here’s some more properties of UIGravityBehavior

UIKit Dynamics GravityBehavior

2.2 UIPushBehavior

Use: Applies a force to its dynamic items to change its position accordingly to its mentioned direction.
UIKit Dynamics Push

To apply force which push the squareView up-side, we need to define the direction as well as with what force/magnitude it pushed.

UIKit Dynamics PushBehavior

2.3 UICollisionBehavior

Use: To manage collisions of dynamic items with each other within the specified boundaries are performed using this behaviour.

UIKit Dynamics Collision
How can we make collisions? Let’s do it by falling multiple views onto each other for fun.

From above code all the views will fall and exit the screen, so we need to set boundary where they can collide.

Or you can just boundary one by one

At last you can set boundary using:

UIKit Dynamics CollisionBehavior

2.4 UISnapBehavior

Use: It tends to behave like a spring, which starts with damping motion and settles at a specific point over the time.
As we all know snapping means performing something quickly or suddenly. This behavior do the same, dynamic item snapped from its position to a snap point as per its damping value.
UIKit Dynamics Snap

Let simply snap the three buttons, create a collection outlet for the buttons and add snap to each buttons by following code:

Buttons will arrive from the top of the screen and snaps to the center point of the button’s position. Damping plays the main role, because its value indicates how many oscillation does dynamic item performs during snapping.
UIKit Dynamics SnapBehavior

2.5 UIAttachmentBehavior

Use: It is an object that binds a relationship between an object and an anchor point, in our scenario it’s going to be a relationship between view and an anchor.

UIAttachmentBehavior behaves in the same way as pendulum. An anchor and the other object that oscillates (move or swing) with respect to anchorPoint.

UIKit Dynamics Attachment
We need squareView to fall and swing it with respect to anchorView.

To perform amazing animation you can use more properties to UIAttachmentBehavior:

UIKit Dynamics AttachmentBehavior

Conclusion : –

Thank you for reading this article, I hope you have learned simple yet cool animations using UIDynamics, you can download project from Github also feel free to contribute in this project.


Pratik Ghadia iOS Developer

I am Pratik Ghadia, iOS Developer at Yudiz Solutions Pvt. Ltd. - a leading iPhone App development company. I always like to find a better and optimum solution to every problem. My goal is to keep learning and keep growing as a developer.

Comments are closed.