Most apps implement simple animations like the ones we have already seen. However, some animations could benefit from a slightly more dynamic approach. This is what UIKit Dynamics are for. With UIKit Dynamics, you can place one or more view in a scene that emulates physics. For instance, you can apply gravity to a certain object, causing it to fall downward on the screen. You can even have objects bumping in to each other; if you assign a mass to your views, this mass is actually taken into account when two objects bump into each other. Or, for instance, when you apply a certain force to an object with very little mass, it will be displaced more than an object with a lot of mass, just like you would expect in the real world.
To see how this all works, let's take a little break from building our contacts app and let's see if we can build a nice physics experiment! Create a new project and name it CradleExperiment. Make sure you configure it so only the landscape orientations are supported. In the Main.Storyboard file, make sure to set the preview to landscape and add three square views to the main view. Make each view about 100 x 100 points and give them a background color. Normally, you would set up constraints to position these views. However, since we're just experimenting right now and our code will be simpler if we don't set up constraints, we're going to skip the constraints for now. Your layout should look similar to the following screenshot:
![](assets/5c18196a-bdcb-42a8-ba84-86abfff5619f.png)
Create @IBOutlets in ViewController.swift for the views you just added and connect them to the storyboard in the same way you did before. You can name the outlets square1, square2, and square3 if you want to follow along with the code samples.
The simplest thing we can do now is to apply some gravity to the views you just added. This will cause them to fall off the screen. Let's see how this works!
First, add the following property to ViewController.swift:
var animator: UIDynamicAnimator!
Then, in viewDidLoad(), add the following:
override func viewDidLoad() {
super.viewDidLoad()
let squares: [UIDynamicItem] = [square1, square2, square3]
animator = UIDynamicAnimator(referenceView: view)
let gravity = UIGravityBehavior(items: squares)
animator.addBehavior(gravity)
}
If you test your app now, you'll notice that your views start falling immediately. Setting up a simple scene like this is really easy with UIKit Dynamics; however, the current scene is not very interesting (yet). Let's see what would happen if our views were somehow attached to the top of the screen with ropes. This should cause them to fall a little and then bounce and swing around until they eventually come to a standstill.
Add the following code to viewDidLoad():
var nextAnchorX = 250
for square in squares {
let anchorPoint = CGPoint(x: nextAnchorX, y: 0)
nextAnchorX -= 30
let attachment = UIAttachmentBehavior(item: square,
attachedToAnchor: anchorPoint)
attachment.damping = 0.7
animator.addBehavior(attachment)
}
If you run the app now, you'll note that every square is now attached to an invisible point on the screen that keeps it from falling. We're still missing a few things though; the squares can now simply cross over each other. It would be a lot cooler if they bumped into each other.
All you need to do is add the following two lines to viewDidLoad():
let collisions = UICollisionBehavior(items: squares)
animator.addBehavior(collisions)
Are you convinced that UIKit Dynamics are cool? I thought so; it's amazing how much you can do with just a little bit of code. Let's add some mass to our squares and make them more elastic to see if this has any effect on how the squares collide.
Update the for loop from before by adding the following code:
for square in squares {
//…
let dynamicBehavior = UIDynamicItemBehavior()
dynamicBehavior.addItem(square)
dynamicBehavior.density = CGFloat(arc4random_uniform(3) + 1)
dynamicBehavior.elasticity = 0.8
animator.addBehavior(dynamicBehavior)
}
If you build and run now, you won't see a huge difference. However, you can tell that the animation has changed because the variables that go into the physics simulation have changed. Even though this example was very simple, you should be able to implement some interesting behaviors by creating an animator and playing around with the different traits you can add to it.
The last stop on our journey through animation-land is view controller transitions! Let's dive right in, shall we?