gpt4 book ai didi

ios - 为什么我的 Lottie 文件没有出现在我的项目中?

转载 作者:行者123 更新时间:2023-11-29 05:26:55 28 4
gpt4 key购买 nike

我正在尝试在我的项目中使用 Lottie 文件;我尝试了很多方法,但都不起作用...什么也没有出现。

我尝试了两种方法:

  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()
}
}

最佳答案

我发现从简单的概念证明开始是很有帮助的,以确保事情正常工作,然后我从那里进行定制。使用示例 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。

关于ios - 为什么我的 Lottie 文件没有出现在我的项目中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58071771/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com