Use Lottie for loading animations

The link to Lottie is lottie-ios

Benefits of using Lottie for animation:
  1. Multi-platform unification, including usage and animation effects
  2. Instead of importing images, you only need a JSON file (only a few KB to tens of KB in size), effectively reducing the size of the application installation package
  3. Programmers can achieve cool animation effects without writing animation code

The two main classes are AnimationView and Animation. AnimationView is the View that executes the Animation, and Animation is the Animation that executes. For example loading an animation:

Create an AnimationView

let animationView: AnimationView = {
    let animationView = AnimationView()
    animationView.size = CGSize(width: 170, height: 170)
    return animationView
}()
Copy the code

Create an Animation instance and assign it to the Animation property of the animationView

let animation = Animation.named("animationName")

animationView.animation = animation
Copy the code

AnimationName is the JSON file name, which can be downloaded here or made yourself.

Now you can perform the animation where you need it

animationView.play(fromProgress: 0, toProgress: 1, loopMode: .loop, completion: nil)
Copy the code

Animations can be terminated or paused

animationView.stop()


animationView.pause()

Copy the code

Finally, here is a loadingView library that uses Lottie. You can download the animation json file import project at LottieFiles website.

Install using POD

pod 'LottieLoadingView'
Copy the code

The import

import LottieLoadingView
Copy the code
/ / set the animation AQLoadingView. Shared. AnimationName ="935-loading"/ / load AQLoadingView. StartLoading () / / processing tasks DispatchQueue. Main. AsyncAfter (deadline: Dispatchtime.now () + 5) {// end aqloadingView.endloading ()}Copy the code

Effect: