英文:
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")!)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论