Skip to content

Instantly share code, notes, and snippets.

@AlexeyTsutsoev
Created April 15, 2023 09:51
Show Gist options
  • Save AlexeyTsutsoev/0e67acfe13c9754b9ccc28d408ec15e0 to your computer and use it in GitHub Desktop.
Save AlexeyTsutsoev/0e67acfe13c9754b9ccc28d408ec15e0 to your computer and use it in GitHub Desktop.
Lottie Adapter for SwiftUI macOS + iOS
import Foundation
/// list animation with name from Resources
public enum AnimationList: String {
/// stub for screens that are still in progress
case inProgress = "in-develop-animation"
/// animation for showing with some errors
case errorAnimation = "error-animation"
/// animation for showing with array
case emptyAnimation = "empty-animation"
}
import Foundation
import Lottie
/// util class for work with Lottie
/// enum generated by SwiftFormat
public enum LottieHelper {
/// func created lottie view for renders in SwiftUI Views by name
static func makeLottieView(name: AnimationList) -> LottieAnimationView {
let animationView = LottieAnimationView()
let animation = LottieAnimation.named(name.rawValue)
animationView.animation = animation
animationView.loopMode = .loop
animationView.play()
animationView.contentMode = .scaleAspectFit
return animationView
}
}
import SwiftUI
struct LottieView: View {
let name: AnimationList
var body: some View {
#if os(macOS)
LottieViewMacOS(name: name)
#else
LottieViewIOS(name: name)
#endif
}
}
struct LottieView_Previews: PreviewProvider {
static var previews: some View {
LottieView(name: .inProgress)
}
}
import SwiftUI
#if os(iOS)
struct LottieViewIOS: UIViewRepresentable {
let name: AnimationList
func makeUIView(context: Context) -> UIView {
let view = UIView()
let animationView = LottieHelper.makeLottieView(name: name)
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: Context) {}
}
struct LottieViewIOS_Previews: PreviewProvider {
static var previews: some View {
LottieViewIOS(name: .inProgress)
}
}
#endif
import SwiftUI
#if os(macOS)
struct LottieViewMacOS: NSViewRepresentable {
let name: AnimationList
func updateNSView(_ nsView: NSViewType, context: Context) {}
func makeNSView(context: Context) -> some NSView {
let view = NSView(frame: .zero)
let animationView = LottieHelper.makeLottieView(name: name)
animationView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(animationView)
animationView.translatesAutoresizingMaskIntoConstraints = false
animationView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
animationView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
animationView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
animationView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
animationView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
animationView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
return view
}
}
struct LottieViewMacOS_Previews: PreviewProvider {
static var previews: some View {
LottieViewMacOS(name: .inProgress)
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment