Adding Tableview Loading Animation In Swift

In my app I’m still working on, I’m displaying contacts from address book in tableview. Because it can take little moment before data are fetched, I decided to add simple loading animation, so that user is not staring at blank screen. Nothing fancy really, but anyway here’s the result:TableView Loading Animation

First I’ve created this image using Pixelmator:

tableViewLoader

Then UIImageView initialization:

And then I’m adding it to UITableView it in viewDidLoad():

I’m using insertSubview() because I want it to be behind tableView cells after they are displayed.

Now how to rotate it? Here I pretty much used code from this article. So I have the same UIView extension:

I don’t trigger the animation with any IBaction, I have it as normal functions, which I call whenever I need:

 

So whenever I’m reloading tableView items, I’ll call self.animateLoader()Func animationDidStop() is called automatically every time rotate360Degrees() animation is finished, so if I want to prevent it from running again, I’ll just set self.shouldStopRotating = true after tableView data are reloaded. Table items are visible now, but behind them there’s still tableViewLoader. It’s not rotating anymore, but let’s hide it with simple:

 

That’s all folks!