Hello! My name is Vladyslav Semenchenko and I am as you can note iOS developer:] This is my first post on Medium so please welcome!

Several weeks ago I started the new iOS project. And after several days of coding I faced the problem — my UIViewController classes have too much code for animations. This is not bad but also not good for clean code as I planning. I want that my UIViewControllers only have code for user interactions and maybe some basic setup. All other logic must be in workers. This is great for testability.

So my question was: how can I move animations outside UIVIewController and make it simple to use and understand? First thing that come to my mind was to create separate class for animations and call functions outside UIVIewController. Let’s see how it works.

Ok, let’s imagine that we have IntroductionViewController. First of all let’s see how it looks like without any changes:

As you see there is two problems:

1) ViewController has variables which it does not need itself

2) There is animation function which can be moved outside

Let’s optimize this code and create separate class for animations:

After those changes you can call in checkUser() function our animation function like:

This is much better. We move our animation block outside. But we have the second problem — how can we move out not needed variables and have them only in animation class? For this we have cool interface builder object — Object! Yep here it is:

Object object:]

We can add this object to our ViewController, set our animation class Custom Class and then link needed variables. Let’s try to do this!

First of all add Object to ViewController:

Add Object to ViewController

Then set Custom Class for Object:

Set Custom Class for Object

And now you can Ctr-drag your UI elements to your animation class:

Add outlets to Animation Class

Also you must link animation object with ViewController. Just Ctr-drag Animation Object to ViewController and set name for outlet.

And here is the final version of InitialViewControllerAnimation class:

And now the final ViewController class:

Much cleaner right? We resolve two issues:

1) Move out animation blocks outside ViewController class

2) Move outside variables which needed for Animation class and not needed for ViewController class

Hope this article will be interesting for readers. Please leave your comments. It will be cool to discuss this issues with community!

P.S. Cool song that I found several days ago: Led Zeppelin — Since I’ve been Loving You — https://itun.es/ru/F-imO?i=674271721