Created
January 27, 2021 18:47
-
-
Save OskarGroth/d959d15ef96eff19ce433077237e37fb to your computer and use it in GitHub Desktop.
This file contains 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
struct ContentView: View { | |
var body: some View { | |
Image(nsImage: NSImage(named: .init(NSImage.applicationIconName))!) | |
.resizable() | |
.frame(width: 100, height: 100) | |
.tapWithHighlight(onTap: { | |
print("Tap") | |
}, onLongPress: { | |
print("Long Press") | |
}) | |
.padding(100) | |
} | |
} | |
struct HighlightTappable: ViewModifier { | |
@State private var isPressed = false | |
var onTap: () -> Void | |
var onLongPress: () -> Void | |
func body(content: Content) -> some View { | |
content | |
.brightness(isPressed ? -0.5 : 0) | |
.onTapGesture { | |
onTap() | |
} | |
.onLongPressGesture(minimumDuration: 2, pressing: { | |
isPressed = $0 | |
}, perform: { | |
onLongPress() | |
}) | |
} | |
} | |
extension View { | |
func tapWithHighlight(onTap: @escaping () -> Void, onLongPress: @escaping () -> Void) -> some View { | |
self.modifier(HighlightTappable(onTap: onTap, onLongPress: onLongPress)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment