Custom Loading Indicator in SwiftUI

To reveal and animate the indicator, press the Unhide/Animate button. Afterwards, utilize my REST API service to send a server request. Upon receiving a success callback or error message after a brief wait time, display a new view. You have the option to switch between a new view and an animation. For instance, consider implementing the Lottie framework for the animation aspect.

activity indicator swiftui

struct ActivityIndicator: UIViewRepresentable {
    @Binding var isAnimating: Bool
    let style: UIActivityIndicatorView.Style
    func makeUIView(context: UIViewRepresentableContext) -> UIActivityIndicatorView {
        return UIActivityIndicatorView(style: style)
    }
    func updateUIView(_ uiView: UIActivityIndicatorView, context: UIViewRepresentableContext) {
        isAnimating ? uiView.startAnimating() : uiView.stopAnimating()
    }
}
//Usage...
ActivityIndicator(isAnimating: .constant(true), style: .large)

Frequently Asked Questions