Sumit Jha
11/28/2023, 11:28 AMAnonymike
11/28/2023, 5:14 PMSumit Jha
11/28/2023, 5:41 PMAnonymike
11/28/2023, 8:04 PMimport Lottie
import SwiftUI
public struct LottieView: UIViewRepresentable {
let name: String
var contentMode: UIView.ContentMode
var loopMode: LottieLoopMode
var speed: CGFloat
public var completionHandler: LottieCompletionBlock?
let animationView = LottieAnimationView()
public init(name: String, contentMode: UIView.ContentMode = .scaleAspectFit, loopMode: LottieLoopMode = .loop, speed: CGFloat = 1.0, completionHandler: LottieCompletionBlock? = nil) {
self.name = name
self.contentMode = contentMode
self.loopMode = loopMode
self.speed = speed
self.completionHandler = completionHandler
}
public func makeUIView(context: UIViewRepresentableContext<LottieView>) -> UIView {
let view = UIView(frame: .zero)
if let animation = LottieAnimation.named(name) {
animationView.animation = animation
animationView.contentMode = contentMode
animationView.loopMode = loopMode
animationView.animationSpeed = speed
animationView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(animationView)
animationView.play(completion: completionHandler)
NSLayoutConstraint.activate([
animationView.heightAnchor.constraint(equalTo: view.heightAnchor),
animationView.widthAnchor.constraint(equalTo: view.widthAnchor)
])
} else {
print("Error loading .lottie")
}
return view
}
public func updateUIView(_ uiView: UIViewType, context: Context) {}
}
Anonymike
11/28/2023, 8:06 PM