在SwiftUI中如何播放SVGA动画

huangapple go评论80阅读模式
英文:

How to play svga animation in SwiftUI

问题

Sure, here's the translation:

我需要支持iOS14,有没有办法在SwiftUI中播放svga动画?
这是我的svga网址:https://iulia.iwlive.club/ng/ring3d3.svga

英文:

I need to support iOS14, is there any way to play svga animation using SwiftUI?
Here is my svga url: https://iulia.iwlive.club/ng/ring3d3.svga

答案1

得分: 0

可以通过 SwiftySVGAPlayer 实现。

首先,使用 CocoaPod 安装 SwiftySVGAPlayer:

pod 'SwiftySVGAPlayer'

然后,实现 UIViewRepresentable 使 SwiftySVGAPlayer 在 SwiftUI 中可用。

import SwiftUI
import SVGAPlayer

struct SVGAPlayerView: UIViewRepresentable {
    var url: URL

    func makeUIView(context: Context) -> SVGAPlayer {
        let player = SVGAPlayer()
        let parser = SVGAParser()
        parser.parse(with: url) { videoItem in
            if let item = videoItem {
                player.videoItem = item
                player.startAnimation()
            }
        }
        return player
    }

    func updateUIView(_ uiView: SVGAPlayer, context: Context) {
    }
}

struct SVGAPlayerView_Previews: PreviewProvider {
    static var previews: some View {
        SVGAPlayerView(url: URL(string: "https://iulia.iwlive.club/ng/ring3d3.svga")!)
    }
}
英文:

It can be achieved by SwiftySVGAPlayer

First, use CocoaPod to install SwiftySVGAPlayer:

pod 'SwiftySVGAPlayer'

Then, implement UIViewRepresentable to make SwiftySVGAPlayer available in SwiftUI

import SwiftUI 
import SVGAPlayer

struct SVGAPlayerView:UIViewRepresentable
{
    var url:URL
    
    func makeUIView(context: Context) -> SVGAPlayer {
        let player = SVGAPlayer()
        let parser = SVGAParser()
        parser.parse(with: url) { videoItem in
            if let item = videoItem {
                player.videoItem = item
                player.startAnimation()
            }
        }
        return player
    }
    
    func updateUIView(_ uiView: SVGAPlayer, context: Context) {
    }
}


struct SVGAPlayerView_Previews: PreviewProvider {
    static var previews: some View {
        SVGAPlayerView(url: URL(string: "https://iulia.iwlive.club/ng/ring3d3.svga")!)
    }
}

huangapple
  • 本文由 发表于 2023年4月13日 17:06:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003640.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定