2022-02-17 01:53:11 +05:30
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct TapRecognizerViewModifier: ViewModifier {
|
|
|
|
@State private var singleTapIsTaped: Bool = .init()
|
|
|
|
|
|
|
|
var tapSensitivity: Double
|
|
|
|
var singleTapAction: () -> Void
|
|
|
|
var doubleTapAction: () -> Void
|
2022-03-28 00:54:32 +05:30
|
|
|
var anyTapAction: () -> Void
|
2022-02-17 01:53:11 +05:30
|
|
|
|
2022-03-28 00:54:32 +05:30
|
|
|
init(
|
|
|
|
tapSensitivity: Double,
|
|
|
|
singleTapAction: @escaping () -> Void,
|
|
|
|
doubleTapAction: @escaping () -> Void,
|
|
|
|
anyTapAction: @escaping () -> Void
|
|
|
|
) {
|
2022-02-17 01:53:11 +05:30
|
|
|
self.tapSensitivity = tapSensitivity
|
|
|
|
self.singleTapAction = singleTapAction
|
|
|
|
self.doubleTapAction = doubleTapAction
|
2022-03-28 00:54:32 +05:30
|
|
|
self.anyTapAction = anyTapAction
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func body(content: Content) -> some View {
|
|
|
|
content.gesture(simultaneouslyGesture)
|
|
|
|
}
|
|
|
|
|
|
|
|
private var singleTapGesture: some Gesture {
|
|
|
|
TapGesture(count: 1).onEnded {
|
2022-03-28 00:54:32 +05:30
|
|
|
anyTapAction()
|
|
|
|
|
2022-02-17 01:53:11 +05:30
|
|
|
singleTapIsTaped = true
|
|
|
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + tapSensitivity) {
|
|
|
|
if singleTapIsTaped {
|
|
|
|
singleTapAction()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var doubleTapGesture: some Gesture {
|
|
|
|
TapGesture(count: 2).onEnded {
|
|
|
|
singleTapIsTaped = false
|
|
|
|
doubleTapAction()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var simultaneouslyGesture: some Gesture {
|
|
|
|
singleTapGesture.simultaneously(with: doubleTapGesture)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension View {
|
2022-03-28 00:54:32 +05:30
|
|
|
func tapRecognizer(
|
|
|
|
tapSensitivity: Double,
|
2022-12-27 00:11:58 +05:30
|
|
|
singleTapAction: @escaping () -> Void = {},
|
|
|
|
doubleTapAction: @escaping () -> Void = {},
|
2022-03-28 00:54:32 +05:30
|
|
|
anyTapAction: @escaping () -> Void = {}
|
|
|
|
) -> some View {
|
|
|
|
modifier(
|
|
|
|
TapRecognizerViewModifier(
|
|
|
|
tapSensitivity: tapSensitivity,
|
|
|
|
singleTapAction: singleTapAction,
|
|
|
|
doubleTapAction: doubleTapAction,
|
|
|
|
anyTapAction: anyTapAction
|
|
|
|
)
|
|
|
|
)
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
|
|
|
}
|