wake-ios/wake/SharedUI/Animation/LottieView.swift

58 lines
1.8 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import SwiftUI
import Lottie
struct LottieView: UIViewRepresentable {
let name: String
let loopMode: LottieLoopMode
let animationSpeed: CGFloat
let isPlaying: Bool
init(name: String, loopMode: LottieLoopMode = .loop, animationSpeed: CGFloat = 1.0, isPlaying: Bool = true) {
self.name = name
self.loopMode = loopMode
self.animationSpeed = animationSpeed
self.isPlaying = isPlaying
}
func makeUIView(context: Context) -> LottieAnimationView {
let animationView = LottieAnimationView()
// 1: 使
if let animation = LottieAnimation.named(name) {
animationView.animation = animation
}
// 2: 1使
else if let path = Bundle.main.path(forResource: name, ofType: "json") {
let animation = LottieAnimation.filepath(path)
animationView.animation = animation
}
//
animationView.loopMode = loopMode
animationView.animationSpeed = animationSpeed
animationView.contentMode = .scaleAspectFit
animationView.backgroundBehavior = .pauseAndRestore
// /
if isPlaying {
animationView.play()
} else {
animationView.pause()
}
return animationView
}
func updateUIView(_ uiView: LottieAnimationView, context: Context) {
// isPlaying /
if isPlaying {
if !uiView.isAnimationPlaying {
uiView.play()
}
} else {
if uiView.isAnimationPlaying {
uiView.pause()
}
}
}
}