Skip to content

Instantly share code, notes, and snippets.

@cbetz
Created January 16, 2020 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbetz/771f18fe88222e2c151a8e09605211f7 to your computer and use it in GitHub Desktop.
Save cbetz/771f18fe88222e2c151a8e09605211f7 to your computer and use it in GitHub Desktop.
Use LottieFiles in your SwiftUI app with this UIView wrapper
// Chris Betz
// Betz Software
//
// Source: https://www.youtube.com/watch?v=fVehE3Jf7K0
// Important: If you get EXC_BAD_INSTRUCTION you need to set DEAD_CODE_STRIPPING = NO in your app's target. More info:
// https://stackoverflow.com/questions/58757949/swift-ui-lottie-thread-1-exc-bad-instruction-code-exc-i386-invop-subcode-0x/58762913#58762913
//
// Check out FlightMaster for an example app: https://apps.apple.com/us/app/flight-master/id1495056152
import SwiftUI
import Lottie
struct LottieUIView: UIViewRepresentable {
let animationView = AnimationView()
var filename = "mountain"
func makeUIView(context: UIViewRepresentableContext<LottieUIView>) -> UIView {
let view = UIView()
let animation = Animation.named(filename)
animationView.animation = animation
animationView.contentMode = .scaleAspectFit
animationView.loopMode = .loop
animationView.play()
animationView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(animationView)
NSLayoutConstraint.activate([
animationView.heightAnchor.constraint(equalTo: view.heightAnchor),
animationView.widthAnchor.constraint(equalTo: view.widthAnchor),
])
return view
}
func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<LottieUIView>) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment