Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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