How to do Simple Animations in iOS using Swift

Yogesh Manghnani
codeburst
Published in
2 min readNov 18, 2018

--

Animations are a core part of the iOS app experience. I know if you are a you might hesitate to bring animations in your app. But don’t worry it’s quite easy to do. Before moving forward keep in mind that the way you are gonna do this is fair basic and not rocket science.

Animations in Swift can be done in multiple ways.

  1. UIView.animate()
  2. CABasicAnimation
  3. Transforms

and many more.

In this article we are going to look forward to one of the easiest for of animations in Swift. This is

UIView.animate(withDuration: TimeInterval, animations: () -> Void)

withDuration : TimeInterval

This is used to tell the time interval of animation. TLDR how long do you want it to run. It is in seconds. It can be anything a float, an int. Just anything. Of course not String 😂 . Anyways back to topic the next argument is a closure.

animations: () -> Void

This argument is a closure. The closure does not have any arguments and does not return anything. This is the main part of the of the animation thing that you wanna do and it is fair and simple. All you have to do here is change the UI properties of whatever view you want and it will happen over time interval of the previous argument you passed. Take a look at the code block below.

let label = UILabel()
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.text = "Hello World!"
label.textColor = .black
UIView.animate(withDuration: 5) {
label.frame = CGRect(x: 150, y: 300, width: 200, height: 20)
}

What this does is really great. Have a look at it below.

Isn't it very easy. You do a hell lot with this. Go try it.

Follow me on Instagram, Facebook, Twitter
Download my iOS App.

--

--

I am fullstack developer who can’t sleep with bugs. (both in bed and in code)