Skip to content

Instantly share code, notes, and snippets.

@OskarGroth
Created January 27, 2021 18:47
Show Gist options
  • Save OskarGroth/d959d15ef96eff19ce433077237e37fb to your computer and use it in GitHub Desktop.
Save OskarGroth/d959d15ef96eff19ce433077237e37fb to your computer and use it in GitHub Desktop.
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