article

Thursday, January 27, 2022

SwiftUI Spinner Loading/Waiting Activity

SwiftUI Spinner Loading/Waiting Activity

ContentView.swift
 
//
//  ContentView.swift
//  swiftuidev15ios
//
//  Created by Cairocoders
//

import SwiftUI

struct ContentView: View {

    @State var show = false
    
    var body: some View {

        ZStack {
            if self.show {
                GeometryReader { geometry in
                    Loader()
                        .position(x: geometry.size.width / 2, y: geometry.size.height / 2)
                }.background(Color.black.opacity(0.45).edgesIgnoringSafeArea(.all))
                    .onTapGesture {
                        self.show.toggle()
                    }
                
            }else {
                Button(action: {
                    self.show.toggle()
                }) {
                    Text("Show Custom Indicator")
                }
            }
        }
        

    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

struct Loader : View {
    
    @State var animate = false
    
    var body: some View {
        VStack {
            Circle()
                .trim(from: 0, to: 0.8)
                .stroke(AngularGradient(gradient: .init(colors: [.red,.orange]), center: .center), style: StrokeStyle(lineWidth: 8, lineCap: .round))
                .frame(width: 45, height: 45)
                .rotationEffect(.init(degrees: self.animate ? 360 : 0))
                .animation(Animation.linear(duration: 0.7).repeatForever(autoreverses: false))
                .padding(.top, 10)
            
            Text("Please Wait...").padding()
        }
        .background(Color.white)
        .cornerRadius(15)
        .onAppear {
            self.animate.toggle()
        }
    }
}

Related Post