Member-only story
How to Animate Auto Layout Constraints
Make it look smooth and nice
Animating constraints is one of those things that I’ve heard are tricky. Activate and reactivate constraints? You can do it! Just read on for the details.
Difficulty: Beginner | Easy | Normal | Challenging
Prerequisites:
- Be able to code Swift using Playgrounds, and display the iOS UI in the same
The task
I’m going to create an attractive green button, and let it move around the screen according to it’s constraints.
I’ve set up the button (with fun constraints) in loadView(). Now I’ve set up this project in Playgrounds. This means that you’ll need import PlaygroundSupport
and since I’m calling my View Controller myViewController
(because I’m excellent at naming) I’ll need PlaygroundPage.current.liveView = MyViewController()
.
At the bottom of this page is the working Playground
code — skip ahead to there if you choose not to have the rather excellent