LoginSignup
10
9

More than 3 years have passed since last update.

LottieでかんたんリッチなアニメーションをiOSのViewにもたらす

Last updated at Posted at 2020-10-26

話題のアニメーションライブラリLottieの導入法をイチから解説。
これであなたのアプリもめっちゃモダンでかわいくなります。
ボタン類も豊富に用意されています。
Oct-27-2020 00-01-19.gif

導入方法

( SwiftPackageManagerバージョンです。手順通りにやれば大丈夫です)

Lottieを導入したいXcodeプロジェクトをひらきます。
Build SettingsLinking から Dead Code StrippingNo にします。
スクリーンショット 2020-10-26 21.33.35.png

File > Swift Packages > Add Package Dependencyを選択します。
スクリーンショット 2020-10-26 21.08.02.png

RepositoryにLottieのGitHub URL: https://github.com/airbnb/lottie-ios をコピペします。
そしてNext
スクリーンショット 2020-10-26 21.26.47.png

ブランチなどもそのままでNext
スクリーンショット 2020-10-26 21.26.58.png

Finish で Lottie フレームワーク がプロジェクトに入ります。
スクリーンショット 2020-10-26 22.05.36.png

使用方法

Lottie アニメーションのJsonファイルを取得

LottieのHPからLottie JSON をダウンロード。
スクリーンショット 2020-10-26 23.01.58.png

プロジェクトにドラッグ&ドロップ。
(ファイル名は好きな名前.jsonに変えて大丈夫)
スクリーンショット 2020-10-27 0.13.07.png

Animation View を追加

Lottie をインポートします。


import Lottie

Lottieアニメーションビューを初期化します。
UIViewと同じようにあつかえます。

var animationView = AnimationView()
animationView.frame = view.frame
animationView.contentMode = .scaleAspectFit
view.addSubview(animationView)

Animationを初期化して再生

var animation = Animation.named("36596-the-woman-sitting-on-the-phone")
// JSONファイルをフォルダにまとめている場合は、 var animation = Animation.named("36596-the-woman-sitting-on-the-phone", subdirectory: "Animations")
animationView.animation = animation
animationView.play()

Oct-26-2020 23-05-26.gif
背景透過で再生されました。

オプション

animationView.stop( ) :停止
・animationView.pause( ) :一時停止
animationView.loopMode :ループモード(Default:.playOnce)
オプション  挙動
.playOnce 一度だけ再生
.loop ループ再生
.autoReverse 再生・逆再生を繰り返す
.repeat(Float) 指定回数繰り返し
.repeatBackwards(Float) 指定回数再生・逆再生
animationView.backgroundBehavior :アプリがバックグラウンドになった時のアニメーションの挙動
オプション  挙動
.pause 一時停止状態で止まる
.pauseAndRestore 一時停止状態で止まり、アプリがフォーグラウンドになるとそこから再生
.stop アニメーションの冒頭に移動
.forthFinish アニメーションの末尾に移動
animationView.play(fromProgress:), animationView.play(toProgress:) :アニメーションの範囲を指定(0〜1)

自前のアニメーションを作る

デザイナー向けチュートリアルで After Effect というソフトを使ってLottieアニメーションを作る方法を学べるそうです。

また、一から作るところまでは行かなくても、既存のLottieアニメーションをLottieエディタにドロップして編集できます。

公式サイトでは、無料・有料アニメーションはじめ、Webに埋め込む方法などたくさん情報があります。
Oct-26-2020 23-56-40.gif

🐣


お仕事のご相談こちらまで
rockyshikoku@gmail.com

Core MLを使ったアプリを作っています。
機械学習関連の情報を発信しています。

Twitter
Medium

10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9