☘️ cocoapodsを使って、Lottieを導入して、アニメーションを表示させます。
☘️ gitHabから、cocoapodsにpodファイルを貼り付けて、インストールする。
☘️ Lottieから、マニメーションのjsonファイルをダウンロードして、xcodに挿入する。
☘️ Mains.toryboardを、一部使いますが、コードによるLabelなどの設置を学ぶ。
import UIKit
import Lottie
class IntroViewController: UIViewController,UIScrollViewDelegate {
@IBOutlet weak var scrollView: UIScrollView!
var onboardArray = ["0","1","2","3","4","5","6"]
var onboardStringArray = ["大器晩成","海鳴りの唄","眦","温故知新","波","北海峡","海で一生終わりたかった"]
override func viewDidLoad() {
super.viewDidLoad()
//ページングが出来る
scrollView.isPagingEnabled = true
setUpScroll()
//Lottie
for i in 0...6 {
let animationView = AnimationView()
let animation = Animation.named(onboardArray[i])
animationView.frame = CGRect(x: CGFloat(i) * view.frame.size.width, y: 0, width: view.frame.size.width, height: view.frame.size.height)
animationView.animation = animation
animationView.contentMode = .scaleAspectFit
animationView.loopMode = .loop
animationView.play()
scrollView.addSubview(animationView)
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//ナビゲーションコントローラーを非表示になる。
self.navigationController?.isNavigationBarHidden = true
}
func setUpScroll(){
//delegate = 自分
scrollView.delegate = self
scrollView.contentSize = CGSize(width: view.frame.size.width * 7, height: view.frame.size.height)
//labelの配置
let animationView = AnimationView()
let animation = Animation.named(onboardArray[i])
animationView.frame = CGRect(x: CGFloat(i) * view.frame.size.width, y: 0, width: view.frame.size.width, height: view.frame.size.height)
//labelのフォント・テキスト
animationView.animation = animation
animationView.contentMode = .scaleAspectFit
animationView.loopMode = .loop
animationView.play()
scrollView.addSubview(animationView)
}
}
}
☘️ まとめ
cocoapodsの扱いに慣れてしまえば、それほど難しくはないと思います。
今回の講義から、コードによるLabelなどの設置を行いました。
簡単ではありませんが、根気強く覚え行こうと思います。