Last active
July 8, 2020 23:20
-
-
Save soffes/6d36fea29f36a44312ae0b133cf8e821 to your computer and use it in GitHub Desktop.
This file contains 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
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