在Lottie中加载动画时出现问题

时间:2019-09-07 19:29:35

标签: swift lottie

我试图在我的Xcode项目中使用Lottie。我从一开始就遇到了一些问题。我在youtube上以及此处都找到了解决方案,但它不能解决问题。我无法启动动画,甚至无法使其可见...

我安装了可可豆荚,添加了Lottie库,下载了一个json文件进行播放。我创建了一个UIImageView并将其设置为AnimationView类

    @IBOutlet weak var animationView: AnimationView!

    let filename = "bb8"

 func startAnimation() {

        let animation = Animation.named(filename)
        animationView.animation = animation
        animationView.contentMode = .scaleAspectFit
        animationView.play()
    }

我也尝试了另一种方法,但是它不起作用

        let path = Bundle.main.path(forResource: "bb8",
                                    ofType: "json") ?? ""
        animationView.animation = Animation.filepath(path)
        animationView.contentMode = .scaleAspectFit
        animationView.play()

我希望我的AnimationView显示除故事板上的内容以外的其他内容。

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题,看来您无法再使用情节提要了。我以编程方式创建了animationView,并具有第二段代码,并且可以正常工作。

    var animationView: AnimationView = {
        var av = AnimationView()

        return av
    }()

    func initUI() {
        self.view.backgroundColor = .white
        animationView.backgroundColor = .white

        view.addSubview(animationView)
        animationView.translatesAutoresizingMaskIntoConstraints = false

        animationView.widthAnchor.constraint(equalToConstant: 250).isActive = true
        animationView.heightAnchor.constraint(equalToConstant: 250).isActive = true
        animationView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
        animationView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
    }
func startAnimation() {
        let path = Bundle.main.path(forResource: "bb8",
                                    ofType: "json") ?? ""
        animationView.animation = Animation.filepath(path)
        animationView.contentMode = .scaleAspectFit
        animationView.loopMode = .loop
        animationView.play()


    }

我设法解决了这个问题,并以编程方式创建了我的第一个UI。 如果其他人遇到相同的问题,并且是Swift和Lottie的新手,则可以使用此代码。