Skip to content

Instantly share code, notes, and snippets.

@simibac
Last active October 24, 2022 15:52
Show Gist options
  • Save simibac/2d1e7107b6ccb15ea00ce1905319899d to your computer and use it in GitHub Desktop.
Save simibac/2d1e7107b6ccb15ea00ce1905319899d to your computer and use it in GitHub Desktop.
SwiftUI with Lottie Animation
import SwiftUI
struct ContentView: View {
@State var play = 0
var body: some View {
VStack{
LottieView(name: "checkmark", play: $play)
.frame(width:100, height:100)
Button("Play"){ self.play += 1 }
}
}
}
import SwiftUI
import Lottie
struct LottieView: UIViewRepresentable {
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
var name: String!
@Binding var play:Int
var animationView = AnimationView()
class Coordinator: NSObject {
var parent: LottieView
init(_ animationView: LottieView) {
self.parent = animationView
super.init()
}
}
func makeUIView(context: UIViewRepresentableContext<LottieView>) -> UIView {
let view = UIView()
animationView.animation = Animation.named(name)
animationView.contentMode = .scaleAspectFit
animationView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(animationView)
NSLayoutConstraint.activate([
animationView.widthAnchor.constraint(equalTo: view.widthAnchor),
animationView.heightAnchor.constraint(equalTo: view.heightAnchor)
])
return view
}
func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<LottieView>) {
animationView.play()
}
}
Copy link

ghost commented Oct 24, 2022

code is not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment