
1.Podfileに以下を追加し、pod install
pod 'lottie-ios'
2.import Lottie
import Lottie
3.コード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
//lottie-ios使用例 //AnimationViewクラスのインスタンス生成 let animationView = AnimationView() //アニメーションをスタートさせるメソッド func staryAnimation() { let animation = Animation.named("example") //アニメーションのサイズを決める animationView.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height/1.5) /*アニメーション名を設定する。 右辺のanimationにはAnimation.named("example")が代入されている*/ animationView.animation = animation animationView.contentMode = .scaleAspectFit animationView.loopMode = .loop//アニメをループさせるか animationView.play()//アニメを走らせる view.addSubview(animationView)//viewに乗っける } //アニメーションをストップさせるメソッド func stopAnimation() { animationView.removeFromSuperview() //親のビューから排除する } |
事前にlottiefilesから任意のjsonファイルをダウンロードし、Xcodeのプロジェクトに追加する必要があります。
その際の.jsonのファイル名が.named("example")と同じになるようにします。
.named("example")であれば「example.json」に直すということです。