我试图区分双和三拍手势识别器在SwiftUI。在基于故事板的应用程序中,它可以使用UIGestureRecognizerDelegate完成,但是在SwiftUI中,我还没有找到任何解决方案。
注意:
以下内容不起作用:
.onTapGesture(count: 3) {
print("Triple Tap!")
}
.onTapGesture(count: 2) {
print("Double Tap!")
}发布于 2021-04-07 03:35:44
要有适合每个人在项目中需要的多个手势,苹果没有什么比正常的手势更好的了,将它们混合在一起达到愿望的姿态有时会变得棘手,这是一个拯救,没有问题或错误!
一个名为tapCountRecognizer__,的自定义零问题手势--我们可以将它应用于任何视图。具有单、双、三tapGesture同时工作。
注意到也可以像这样使用,只用于doubleTap和tripleTap__:
.tapCountRecognizer(tapSensitivity: 250, doubleTapAction: doubleTapAction, tripleTapAction: tripleTapAction)或:
.tapCountRecognizer(doubleTapAction: doubleTapAction, tripleTapAction: tripleTapAction)

import SwiftUI
struct ContentView: View {
var body: some View {
Circle()
.fill(Color.red)
.frame(width: 100, height: 100, alignment: .center)
.tapCountRecognizer(tapSensitivity: 250, singleTapAction: singleTapAction, doubleTapAction: doubleTapAction, tripleTapAction: tripleTapAction)
.shadow(radius: 10)
.statusBar(hidden: true)
}
func singleTapAction() { print("singleTapAction") }
func doubleTapAction() { print("doubleTapAction") }
func tripleTapAction() { print("tripleTapAction") }
}struct TapCountRecognizerModifier: ViewModifier {
let tapSensitivity: Int
let singleTapAction: (() -> Void)?
let doubleTapAction: (() -> Void)?
let tripleTapAction: (() -> Void)?
init(tapSensitivity: Int = 250, singleTapAction: (() -> Void)? = nil, doubleTapAction: (() -> Void)? = nil, tripleTapAction: (() -> Void)? = nil) {
self.tapSensitivity = ((tapSensitivity >= 0) ? tapSensitivity : 250)
self.singleTapAction = singleTapAction
self.doubleTapAction = doubleTapAction
self.tripleTapAction = tripleTapAction
}
@State private var tapCount: Int = Int()
@State private var currentDispatchTimeID: DispatchTime = DispatchTime.now()
func body(content: Content) -> some View {
return content
.gesture(fundamentalGesture)
}
var fundamentalGesture: some Gesture {
DragGesture(minimumDistance: 0.0, coordinateSpace: .local)
.onEnded() { _ in tapCount += 1; tapAnalyzerFunction() }
}
func tapAnalyzerFunction() {
currentDispatchTimeID = dispatchTimeIdGenerator(deadline: tapSensitivity)
if tapCount == 1 {
let singleTapGestureDispatchTimeID: DispatchTime = currentDispatchTimeID
DispatchQueue.main.asyncAfter(deadline: singleTapGestureDispatchTimeID) {
if (singleTapGestureDispatchTimeID == currentDispatchTimeID) {
if let unwrappedSingleTapAction: () -> Void = singleTapAction { unwrappedSingleTapAction() }
tapCount = 0
}
}
}
else if tapCount == 2 {
let doubleTapGestureDispatchTimeID: DispatchTime = currentDispatchTimeID
DispatchQueue.main.asyncAfter(deadline: doubleTapGestureDispatchTimeID) {
if (doubleTapGestureDispatchTimeID == currentDispatchTimeID) {
if let unwrappedDoubleTapAction: () -> Void = doubleTapAction { unwrappedDoubleTapAction() }
tapCount = 0
}
}
}
else {
if let unwrappedTripleTapAction: () -> Void = tripleTapAction { unwrappedTripleTapAction() }
tapCount = 0
}
}
func dispatchTimeIdGenerator(deadline: Int) -> DispatchTime { return DispatchTime.now() + DispatchTimeInterval.milliseconds(deadline) }
}extension View {
func tapCountRecognizer(tapSensitivity: Int = 250, singleTapAction: (() -> Void)? = nil, doubleTapAction: (() -> Void)? = nil, tripleTapAction: (() -> Void)? = nil) -> some View {
return self.modifier(TapCountRecognizerModifier(tapSensitivity: tapSensitivity, singleTapAction: singleTapAction, doubleTapAction: doubleTapAction, tripleTapAction: tripleTapAction))
}
}发布于 2021-04-08 10:10:25
这是正确的方式,如果我可以这样说自己!
在iOS 14.4.2,Xcode 12.5beta 3上进行测试。
使用此tapGesture后,只需一次点击就什么都不会发生,双击之后将看到控制台中打印的"double tap",三次或更多点击后将看到控制台中打印的"triple tap"。
struct ContentView: View {
var tapGesture: some Gesture {
SimultaneousGesture(TapGesture(count: 2), TapGesture(count: 3))
.onEnded { gestureValue in
if gestureValue.second != nil {
print("triple tap!")
} else if gestureValue.first != nil {
print("double tap!")
}
}
}
var body: some View {
Text("Hello World!")
.gesture(tapGesture)
}
}发布于 2020-11-29 21:43:28
import SwiftUI
struct MultipleTaps: View {
let doubleTap = TapGesture(count: 2).onEnded({print("Double Tap!")})
let tripleTap = TapGesture(count: 3).onEnded({print("Triple Tap!")})
var body: some View {
let tripleBeforedouble = tripleTap.exclusively(before: doubleTap)
return Text("Hello World!").gesture(tripleBeforedouble)
}
}
struct MultipleTaps_Previews: PreviewProvider {
static var previews: some View {
MultipleTaps()
}
}https://stackoverflow.com/questions/65062833
复制相似问题