Skip to content

Instantly share code, notes, and snippets.

@soffes
Last active July 8, 2020 23:20
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 soffes/6d36fea29f36a44312ae0b133cf8e821 to your computer and use it in GitHub Desktop.
Save soffes/6d36fea29f36a44312ae0b133cf8e821 to your computer and use it in GitHub Desktop.
import SwiftUI
import UIKit
public struct ActivityIndicator: UIViewRepresentable {
// MARK: - Properties
public let style: UIActivityIndicatorView.Style
@Binding public var isAnimating: Bool
private var color: UIColor?
// MARK: - Initializers
public init(style: UIActivityIndicatorView.Style = .medium, isAnimating: Binding<Bool> = .constant(true)) {
self.style = style
_isAnimating = isAnimating
}
// MARK: - UIViewRepresentable
public func makeUIView(context: UIViewRepresentableContext<ActivityIndicator>) -> UIActivityIndicatorView {
UIActivityIndicatorView(style: style)
}
public func updateUIView(_ view: UIActivityIndicatorView, context: UIViewRepresentableContext<ActivityIndicator>) {
isAnimating ? view.startAnimating() : view.stopAnimating()
view.color = color
}
// MARK: - Modifiers
public func color(_ color: UIColor?) -> some View {
var view = self
view.color = color
return view
}
}
#if DEBUG
struct ActivityIndicator_Previews: PreviewProvider {
static var previews: some View {
ActivityIndicator()
.color(.red)
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment