Sweet & Swifty Animations for iOS

Escape the Pyramid of DOOM!

Reid Chatham
Daily Coder
2 min readDec 6, 2016

--

PYRAMID OF DOOM!!!! — ACTUAL SCREENSHOT FROM A FRIEND’S CODE

“In computer science, a data structure is a particular way of organizing data in a computer so that it can be used efficiently.” — Wikipedia

These data structures and design patterns let you escape the infamous anti-patterns with simple and generally useful solutions. Enter SwiftyAnimate (The Plug 🔌).

A better way to animate…

Have you ever tried to string together multiple animations? Yes, of course you have. You wrap each subsequent animation in the completion handler of the previous one and quickly end up writing additional functions just to break up the pyramid of doom (or wormhole of death?). No matter what you end up with it’s not really what you want. Maybe something like this…?

Pyramid of DOOM!

It’s even in the Apple Developer Documentation!!!

https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/AnimatingViews/AnimatingViews.html

Enter Queues to the rescue! They give you O(1) time complexity for both enqueueing and dequeueing which you DO NOT get with the standard Array type in Swift (Arrays have O(1) average time complexity for append and popLast operations, where popFirst is an O(n) operation).

Now we just add our animations to the queue…

…and recursively call the queue in each animation’s completion handler until it is empty.

Easy enough right?

If you want to take it further check this out. In SwiftyAnimate I wrapped our Queue struct in an Animate class. The Animate class enqueues animations, with the .then(duration: TimeInterval, animations: Animation) method, to a series of operations defined by the Operation enum (with animations being one of the cases). __the_code__

Enjoy writing beautiful code! 🎉

Before
After

(Real life example)

--

--

Reid Chatham
Daily Coder

Just dreaming of pizza…. ☁️🍕☁️🍕☁️🍕☁️ http://reidchatham.com