Mike Bluestein November 7, 2013

The graphics and animation frameworks in iOS have always allowed application developers to create engaging user experience and when combined with touch, applications can offer very realistic effects. The introduction of UIKit Dynamics in iOS 7 continues this trend of making it easy for developers to create realistic user interactions. UIKit Dynamics brings a 2D physics engine to UIKit, allowing applications to create rich animations that are modeled with physics.

One example is a snapping effected created with UIKit Dynamics, which you can see in the following video:

Physics in iOS 7 are modeled declaratively with behaviors, which include various physics concepts such as gravity, forces, elasticity and collisions. These behaviors are then added to a UIDynamicAnimator instance which applies physics to items, such as views, that are encapsulated by the various behaviors.

Adding physics to your application with UIKit Dynamics is extremely easy and can be added with just a few lines of code. Let’s look at how to implement the example shown above to snap an image into place.

To work with UIKit Dynamics, the first thing we need need to do is create a UIDynamicAnimator:

The code to drag and drop the image view is done with a UIPanGestureRecognizer as shown below:

When the gesture ends, we check to see if the final touch point is in the area deemed the “drop region”, and if so we will create a UISnapBehavior for the image view at the center of the snap area:

When we add the behavior to the UIDynamicAnimator instance, the image view snaps into place with an awesome magnetic-like effect.

You can download the code for this post from my GitHub repo here to see how easy it is to add physics effects to your Xamarin.iOS applications.