-
-
Save haikusw/88759c8cb7cd90f61c2bc4f77abdef69 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