我创建了一个@IBOutlet weak var animationView: AnimationView!
,然后在ViewController上添加了一个UIView
,将其类从UIView
更改为AnimationView
。在连接插座后,我在班级的viewDidLoad()
中添加以下代码:
let animation = Animation.named("sticky", subdirectory: "Lottie-files")
animationView.animation = animation
animationView.loopMode = .loop
animationView.contentMode = .scaleAspectFill
然后在viewDidAppear()
中添加:
animationView.play()
但是当我运行它时,什么也没有显示。我也在终端中看到了这一点:
[Storyboard] Interface Builder文件中的未知类AnimationView。
但是动画仍然没有出现。没有警告,没有错误,只是不显示。
答案 0 :(得分:1)
viewDidAppear
或之后的 viewDidLoad
中开始动画,例如:viewWillAppear
public override func viewDidLoad() {
super.viewDidLoad()
addAnimation(to: animationView, name: "sticky")
}
public override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
animationView.play()
}
private func addAnimation(to view: AnimationView, name: String) {
let animation = Animation.named(name, subdirectory: "Lottie-files")
view.animation = animation
view.loopMode = .loop
view.contentMode = .scaleAspectFill
}
奖励
Ekramul Hoque's Article about View Controller's Lifecycle
您应该在Lotie
右栏中的视图的Identity Inspector
部分中写入Interface Builder
。
答案 1 :(得分:0)
根据彩票文件,您应致电
play()
viewDidAppear内部的功能。
public override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
animationView.play()
}
请检查以下链接
https://github.com/airbnb/lottie-ios/blob/master/Example/lottie-swift/ViewController.swift
注意:在某些情况下,您需要检查子目录问题
您需要添加文件夹参考
不是网上论坛
有关此部分的更多信息,请检查以下链接。
http://www.thomashanning.com/xcode-groups-folder-references/
快乐编码:)
答案 2 :(得分:0)
您可以通过编程方式设置
import Lottie
然后在视图控制器中添加动画视图,在情节提要中设置lottie动画-> animationView->类-> AnimationView和模块-> Lottie
@IBOutlet weak var animationView: AnimationView!
//Initialise a Lottie view with frame
let customAnimationView = AnimationView(name: "Your lotti file name")
customAnimationView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
//Do your configurations
customAnimationView.loopMode = .loop
customAnimationView.backgroundBehavior = .pauseAndRestore
//And play
customAnimationView.play()
animationView.addSubview(customAnimationView)