Created
July 27, 2020 13:55
-
-
Save roddymunro/61992499cb449ad48cce014aea263418 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
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