Created
November 16, 2020 18:42
-
-
Save halilyuce/caa43f8bb5d836e3cafb1a651b838f43 to your computer and use it in GitHub Desktop.
SwiftUI Lottie Animation View
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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