article

Thursday, January 27, 2022

SwiftUI Spinner Loading/Waiting Activity

SwiftUI Spinner Loading/Waiting Activity

ContentView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
//  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