Created
February 20, 2021 15:42
-
-
Save prafullakumar/394da6c5f128c5758bc189bee4d87ea0 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 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