Skip to content

Instantly share code, notes, and snippets.

@harryhan24
Last active February 23, 2020 08:17
Show Gist options
  • Select an option

  • Save harryhan24/68191bd7c90176a9c9dcabb039429443 to your computer and use it in GitHub Desktop.

Select an option

Save harryhan24/68191bd7c90176a9c9dcabb039429443 to your computer and use it in GitHub Desktop.
import SwiftUI
import Lottie
struct LottieView: UIViewRepresentable {
//makeCoordinator를 구현하여 제약사항을 구현합니다.
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
//json파일명을 받을 프로퍼티
var filename: String
//lottie View
var animationView = AnimationView()
class Coordinator: NSObject {
var parent: LottieView
init(_ animationView: LottieView) {
//frame을 LottieView로 할당합니다.
self.parent = animationView
super.init()
}
}
func makeUIView(context: UIViewRepresentableContext<LottieView>) -> UIView {
let view = UIView()
//lottie 구현뷰
animationView.animation = Animation.named(filename)
animationView.contentMode = .scaleAspectFit
animationView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(animationView)
NSLayoutConstraint.activate([
animationView.widthAnchor.constraint(equalTo: view.widthAnchor),
animationView.heightAnchor.constraint(equalTo: view.heightAnchor)
])
//애니메이션이 계속 반복되게합니다.
animationView.loopMode = .loop
animationView.play()
return view
}
//updateView가 구현되어있지않습니다.
func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<LottieView>) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment