我需要支持iOS14,有没有办法使用SwiftUI播放svga动画?这是我的svga网址:https://iulia.iwlive.club/ng/ring3d3.svga
可以通过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")!)
}
}