Chapter 13. UI Dynamics

UI Dynamics allow you to create very nice effects on your UI components, such as gravity and collision detection. Let’s say that you have two buttons on the screen that the user can move around. You could create opposing gravity fields on them so that they repel each other and cannot be dragged into each other. Or, for instance, you could provide a more live UI by creating a turbulence field under all your UI components so that they move around automatically ever so slightly (or through a noise field, as described in Recipe 13.4) even when the user is not interacting with them. All of this is possible with the tools that Apple has given you in UIKit. You don’t have to use any other framework to dig into UI Dynamics.

One of the basic concepts in UI Dynamics is an animator. Animator objects, which are of type UIDynamicAnimator, hold every other effect together and orchestrate all the effects. For instance, if you have collision detection and gravity effects, the animator decides how the pull on an object through gravity will work hand in hand with the collision detection around the edges of your reference view.

Reference views are like canvases where all your animations happen. Effects are added to views and then added to an animator, which itself is placed on a reference view. In other words, the reference view is the canvas and the views on your UI (like buttons, lables, etc.) will have effects.

13.1 Adding a Radial Gravity Field to Your UI

Problem

Get iOS 9 Swift Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.