Skip to content

Instantly share code, notes, and snippets.

@halilyuce
Created November 16, 2020 18:42
Show Gist options
  • Save halilyuce/caa43f8bb5d836e3cafb1a651b838f43 to your computer and use it in GitHub Desktop.
Save halilyuce/caa43f8bb5d836e3cafb1a651b838f43 to your computer and use it in GitHub Desktop.
SwiftUI Lottie Animation View
//
// LottieView.swift
// InstaSwap
//
// Created by Halil Yuce on 15.11.2020.
//
import SwiftUI
import Lottie
struct LottieView: UIViewRepresentable {
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
var name: String!
var loop:Bool? = false
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
if loop!{
animationView.loopMode = .loop
}else{
animationView.loopMode = .autoReverse
}
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()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment