Skip to content

Instantly share code, notes, and snippets.

@prafullakumar
Created February 20, 2021 15:42
Show Gist options
  • Save prafullakumar/394da6c5f128c5758bc189bee4d87ea0 to your computer and use it in GitHub Desktop.
Save prafullakumar/394da6c5f128c5758bc189bee4d87ea0 to your computer and use it in GitHub Desktop.
import SwiftUI
struct OverlayDemo: View {
@State private var showOverlay = false
var body: some View {
Text("Hello, World!").overlay(overlayView: Text("OverlyText").background(Color.red).onTapGesture {
withAnimation {
showOverlay.toggle()
}
}
, show: $showOverlay)
.onTapGesture {
withAnimation {
showOverlay = true
}
}
}
}
struct Overlay<T: View>: ViewModifier {
@Binding var show:Bool
let overlayView: T
func body(content: Content) -> some View {
ZStack {
content
if show {
overlayView
}
}
}
}
extension View {
func overlay<T: View>( overlayView: T, show: Binding<Bool>) -> some View {
self.modifier(Overlay.init(show: show, overlayView: overlayView))
}
}
struct OverlayDemo_Previews: PreviewProvider {
static var previews: some View {
OverlayDemo()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment