Created
November 1, 2020 17:56
-
-
Save gabrieltheodoropoulos/59dc104163b5718018c6c55309b110fb to your computer and use it in GitHub Desktop.
PressActions modifier - Handle press and release events in SwiftUI
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 PressActions: ViewModifier { | |
var onPress: () -> Void | |
var onRelease: () -> Void | |
func body(content: Content) -> some View { | |
content | |
.simultaneousGesture( | |
DragGesture(minimumDistance: 0) | |
.onChanged({ _ in | |
onPress() | |
}) | |
.onEnded({ _ in | |
onRelease() | |
}) | |
) | |
} | |
} | |
extension View { | |
func pressAction(onPress: @escaping (() -> Void), onRelease: @escaping (() -> Void)) -> some View { | |
modifier(PressActions(onPress: { | |
onPress() | |
}, onRelease: { | |
onRelease() | |
})) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unfortunately this is cancelling the scroll in a scrollview if you move your finger on the modified view