Creating Amazing IOS Animation with Lottie

#<b>INTRODUCTION</b>
Hello everyone , So today's topic we are going to talk about IOS animation and we're going to create an sample app along with the article as well. This time we will using third party library which is <b>Lottie</b>.

###<b>1. WHAT IS LOTTIE?</b>
<b>Lottie</b> is a third party library which develop by <b>Airbnb</b>. It renders After Effects animations. Animations are exported as JSON files through an open-source After Effects extension called <b>Bodymovin</b>. Lottie loads the animation data in JSON format and by using that JSON file, it give us the ability to deploy the after effect animation in our IOS App.

###<b>2. HOW CAN WE USE LOTTIE?</b>
To use <b>Lottie</b> We need the animation JSON file that we export from <b>After effect</b> using <b>Bodymovin</b> but if we don't have or don't know how to use after effect, we also can use <b>Lottie's Predefine Animation</b> from <a href = https://www.lottiefiles.com/?page=1> LottieFile website </a> Go ahead and download some Lottie animation!.

###<b>3. SAMPLE APP USING LOTTIE</b>

Ok. Now let create Xcode Project.After Create the project we need install Lottie via Cocoa Pod. Below is my pod file.

<img width="585" alt="Screen Shot 2018-06-01 at 5.42.35 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/74775366-3aa4-cf04-ce3f-22d6760640cc.png">

Do not confuse to use lottie we need to install only 1 pod which is <b>lottie-ios</b>.
1. <b>lottie-ios</b> : Lottie Library - For Swift Animation.
2. <b>SnapKit</b> : SnapKit Library - For Swift Auto Layout.
Ok now let open the project and drag the Lottie JSON File in.

<b>*APPLICATION BREIF*</b>
So this app, There will be :
1. <b>Animation Object</b> : Display the animation(Lottie JSON file).
2. <b>Button</b> : The animation will start when user press button Click.
3. <b>Slider</b> : User could control Animation Speed by drag slider
4. <b>Label</b> : Show the animation Speed (Slider Value)

<b>ViewController</b>

```swift
import UIKit
import Lottie // Animation Library
import SnapKit // Layout Library

class ViewController: UIViewController {
    
    var animation : LOTAnimationView!
    var button : UIButton!
    var label : UILabel!
    var uiSlider : UISlider!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        createComponent()
        addSupview()
        setupLayout()

    }
}
```


As Usual, There are 3 callback methods in the view controller to setup our UI.
<b>createComponent Function</b>

```swift
// MARK: - Create UI Component
extension ViewController {
    func createComponent() -> Void {
     
        //Lottie
        animation = LOTAnimationView(name: "data")
      
        //Label
        label = UILabel()
        label.textAlignment = .center
        label.text = "50"
        
        //Slider
        uiSlider = UISlider()
        uiSlider.minimumValue = 1
        uiSlider.maximumValue = 100
        uiSlider.value = 50
        uiSlider.isContinuous = true
        
        //Button
        button = UIButton()
        button.setTitle("START", for: .normal)
        button.backgroundColor = UIColor.blue
    }
}
```
In above class , I'm going to explain about LOTanimation only since I have explain many time about the creation of another component. So if you don't understand please go to see my previous article. 

```
animation = LOTAnimationView(name: "data")
```
Here we are creating Lottie Object and passing the Lottie JSON file along (data is Animation JSON file's name).

<b>addSupview Function</b>

```swift
// MARK: - Add Supview
extension ViewController {
    func addSupview() -> Void {
        self.view.addSubview(label)
        self.view.addSubview(animation)
        self.view.addSubview(button)
        self.view.addSubview(uiSlider)
    }
}
```
<b>setupLayout Function</b>

```swift
// MARK: - Setup Layout
extension ViewController {
    func setupLayout() -> Void {
        
        //Lottie
        animation.snp.makeConstraints { (make) in
            make.width.equalToSuperview()
            make.height.equalToSuperview()
        }

        //Label
        label.snp.makeConstraints { (make) in
            make.width.equalToSuperview()
            make.height.equalTo(40)
            make.centerX.equalTo(self.view.snp.centerX)
            make.bottom.equalTo(uiSlider.snp.top)
        }
        
        //Slider
        uiSlider.snp.makeConstraints { (make) in
            make.width.equalTo(300)
            make.height.equalTo(40)
            make.centerX.equalTo(self.view.snp.centerX)
            make.bottom.equalToSuperview().offset(-20)
        }
        
        //Button
        button.snp.makeConstraints { (make) in
            make.width.equalTo(100)
            make.height.equalTo(30)
            make.centerX.equalTo(self.view.snp.centerX)
            make.bottom.equalTo(label.snp.top)
        }
    }
}
```

Ok Now we have completed setting up the UI now let add some action to Button and Slider.

<b>ViewController</b>

```swift
import UIKit
import Lottie // Animation Library
import SnapKit // Layout Library

class ViewController: UIViewController {
    
    var animation : LOTAnimationView!
    var button : UIButton!
    var label : UILabel!
    var uiSlider : UISlider!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        createComponent()
        addSupview()
        setupLayout()

        //Slider Action
        uiSlider.addTarget(self, action: #selector(valueChange), for: .valueChanged)
        //Button Action
        button.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
    }
    
    //Slider Drag
    @objc func valueChange() -> Void {
        
        //Bind Slider Value to Label
        label.text = String(Int(round(uiSlider.value)))
        
        //Bind With AnimationSpeed
        let speed = round(uiSlider.value) / 100
        animation.animationSpeed = CGFloat(speed)
    }
    
    //Button Click
    @objc func btnClick() -> Void {
        animation.stop()
        animation.play()
    }
    
}
```
<b>Action Explain</b>
1. <b>Slider</b> : Slider action is , The slider will trigger when the user drag the slider and we are passing the slider value to the label and we also control the animation speed.
2. <b>Button</b> : Button action is , The animation will start when the button is click.

YES! That's it guys and for more detail you could see at Lottie Github.
https://github.com/airbnb/lottie-ios