Skip to content

Instantly share code, notes, and snippets.

@roddymunro
Created July 27, 2020 13:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roddymunro/61992499cb449ad48cce014aea263418 to your computer and use it in GitHub Desktop.
Save roddymunro/61992499cb449ad48cce014aea263418 to your computer and use it in GitHub Desktop.
import SwiftUI
struct LoadingView<Content>: View where Content: View {
@Binding var isShowing: Bool
var content: () -> Content
var text: String?
var body: some View {
GeometryReader { geometry in
ZStack(alignment: .center) {
content()
.disabled(isShowing)
.blur(radius: isShowing ? 2 : 0)
if isShowing {
Rectangle()
.fill(Color.black).opacity(isShowing ? 0.6 : 0)
.edgesIgnoringSafeArea(.all)
VStack(spacing: 48) {
ProgressView().scaleEffect(2.0, anchor: .center)
Text(text ?? "Loading...").font(.title).fontWeight(.semibold)
}
.frame(width: 250, height: 200)
.background(Color.white)
.foregroundColor(Color.primary)
.cornerRadius(16)
}
}
}
}
}
struct ContentView: View {
@State var loadingViewShowing = false
var body: some View {
LoadingView(isShowing: $loadingViewShowing) {
Button(action: {
loadingViewShowing = true
// mock some network request or other task
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
loadingViewShowing = false
}
}, label: {
Text("Tap Me!")
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment