Nik*_*nov 7 uigesturerecognizer ios swift swiftui
我正在尝试区分 SwiftUI 中的双击和三次点击手势识别器。在基于故事板的应用程序中,可以使用 来完成UIGestureRecognizerDelegate,但在 SwiftUI 中我还没有找到任何解决方案。
笔记:
以下不起作用:
.onTapGesture(count: 3) {
print("Triple Tap!")
}
.onTapGesture(count: 2) {
print("Double Tap!")
}
Run Code Online (Sandbox Code Playgroud)
在 iOS 14.4.2、Xcode 12.5 beta 3 上测试。
使用此点击手势,仅点击一次后不会发生任何事情,双击后您将"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)
}
}
Run Code Online (Sandbox Code Playgroud)
为了拥有一些适合项目中每个人需求的多手势,Apple 没有提供比普通手势更好的功能,将它们混合在一起以达到所需的手势有时会很棘手,这是一个救赎,工作没有问题或错误!
一个名为tapCountRecognizer的自定义零问题手势,我们可以将其应用到任何视图。用于同时进行单次、两次和三次点击手势协同工作。
请注意,您也可以像这样使用,仅适用于doubleTap和TripleTap:
.tapCountRecognizer(tapSensitivity: 250, doubleTapAction: doubleTapAction, tripleTapAction: tripleTapAction)
Run Code Online (Sandbox Code Playgroud)
或者:
.tapCountRecognizer(doubleTapAction: doubleTapAction, tripleTapAction: tripleTapAction)
Run Code Online (Sandbox Code Playgroud)
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") }
}
Run Code Online (Sandbox Code Playgroud)
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) }
}
Run Code Online (Sandbox Code Playgroud)
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))
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1822 次 |
| 最近记录: |