我正在尝试在我的项目中使用彩票文件,但我尝试了许多方法,但没有人可以使用。什么都没出现
在下面的代码中,我尝试了两种方式1- lottieAnimation()
,2-`setupAnimation()以及通过UIView进行尝试,希望您能帮助您了解问题所在并采取正确的步骤来成功使用它。
import UIKit
import Lottie
class BonusVC: UIViewController {
@IBOutlet weak var containerAnim: UIView!
var animation : AnimationView?
let animationView = AnimationView()
override func viewDidLoad() {
super.viewDidLoad()
lottieAnimation()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
lottieAnimation()
//setupAnimation()
}
func setupAnimation() {
animation = AnimationView(name: "wallet")
animation?.frame = self.containerAnim.frame
self.containerAnim.addSubview(animation!)
animation?.loopMode = .autoReverse
animation?.contentMode = .scaleAspectFit
animation?.play()
}
func lottieAnimation() {
let animation = Animation.named("wifi", subdirectory: "LottieAnimation")
animationView.animation = animation
animationView.contentMode = .scaleAspectFit
view.addSubview(animationView)
animationView.backgroundBehavior = .pauseAndRestore
animationView.translatesAutoresizingMaskIntoConstraints = false
animationView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor).isActive = true
animationView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
animationView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -12).isActive = true
animationView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
animationView.setContentCompressionResistancePriority(.fittingSizeLevel, for: .horizontal)
animationView.play()
}
}
答案 0 :(得分:0)
我发现从简单的概念验证入手是很有帮助的,以确保一切正常,然后从那里进行自定义。使用示例Lottie文件之一尝试以下代码:
import UIKit
import Lottie
class ViewController: UIViewController {
let testAnimation = "bb8"
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func showAnimation(_ sender: UIButton) {
let animationView = AnimationView()
let animation = Animation.named(testAnimation)
animationView.frame = CGRect(x: 0, y: 100, width: self.view.frame.size.width, height: 350)
animationView.contentMode = .scaleAspectFit
animationView.animation = animation
animationView.loopMode = .loop
self.view.addSubview(animationView)
animationView.play()
}
}
bb8是JSON文件的名称。我正在使用Swift Package Manager引入Lottie-ios,但这也适用于CocoaPods。