-
-
Save gabrieltheodoropoulos/59dc104163b5718018c6c55309b110fb to your computer and use it in GitHub Desktop.
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() | |
})) | |
} | |
} |
Thanks for this, @gabrieltheodoropoulos. Wondering if you or @AdamWhitcroft ever came up with a solution to his question regarding haptics?
Hi @jesseauciello. As it seems other tasks took priority and I totally neglected this. Sorry for that, I'll try to work on it the soonest.
Hi, thanks for the article "Handle Press And Release Events in SwiftUI". I will try to use ButtonStyle or PrimitiveButtonStyle instead. The configuration.isPressed can be used for checking if the button is pressed or released. /Martin
how to implement both on tap gesture and pressAction .
Hi @NyiYe , Are you talking about buttons? Can it work for you to make a custom ButtonStyle and use isPressed? You have the action in the button for the tap gesture. I attach an article "Creating Custom Button Styles In SwiftUI" from Gabriel. (You can maybe explain you needs more?) /Martin
Unfortunately this is cancelling the scroll in a scrollview if you move your finger on the modified view
Hi @AdamWhitcroft, thanks for your comment. That's an interesting point, I'll check it out at first chance and will get back hopefully with a solution.