为什么UILongPressGestureRecognizer在iOS模拟器中工作但不适用于iPhone(Why UILongPressGestureRecognizer work in iOS simulator but not work for iPhone)

我试图在自定义键盘的删除按钮中添加手势,以确保何时长按系统将作为系统删除按钮,连续执行proxy.deleteBackward()删除。

以下是补充:

var deleteButtonTimer: NSTimer? let deleteButtonLongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "handleLongPressForDeleteButtonWithGestureRecognizer:") deleteButton.addGestureRecognizer(deleteButtonLongPressGestureRecognizer)

接着:

func handleLongPressForDeleteButtonWithGestureRecognizer(gestureRecognizer: UILongPressGestureRecognizer) { switch gestureRecognizer.state { case .Began: print("long press confirmed") if deleteButtonTimer == nil { deleteButtonTimer = NSTimer(timeInterval: 0.1, target: self, selector: "handleDeleteButtonTimerTick:", userInfo: nil, repeats: true) deleteButtonTimer!.tolerance = 0.01 NSRunLoop.mainRunLoop().addTimer(deleteButtonTimer!, forMode: NSDefaultRunLoopMode) } default: print("timer not added as expected") deleteButtonTimer?.invalidate() deleteButtonTimer = nil } }

它在iOS模拟器中完美运行:长按“删除按钮”“长按确认”仅打印一次,代码addTimer()执行并删除按预期连续执行。 但是当我在iPhone中运行时,系统打印“长按确认”一次,然后连续打印“未按预期添加计时器”。 似乎没有执行NSRunLoop.mainRunLoop()。addTimer(deleteButtonTimer!,forMode:NSDefaultRunLoopMode)。

为什么UILongPressGestureRecognizer在iOS模拟器中工作但不适用于iPhone。

顺便说一下,我使用的是Xcode 7.2和iPhone6S iOS9.2。

I am trying to add gesture into my Delete Button in a custom keyboard to ensure when it get long pressed system will work as System Delete Button which continuously execute proxy.deleteBackward() to delete.

Below is added:

var deleteButtonTimer: NSTimer? let deleteButtonLongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "handleLongPressForDeleteButtonWithGestureRecognizer:") deleteButton.addGestureRecognizer(deleteButtonLongPressGestureRecognizer)

and then:

func handleLongPressForDeleteButtonWithGestureRecognizer(gestureRecognizer: UILongPressGestureRecognizer) { switch gestureRecognizer.state { case .Began: print("long press confirmed") if deleteButtonTimer == nil { deleteButtonTimer = NSTimer(timeInterval: 0.1, target: self, selector: "handleDeleteButtonTimerTick:", userInfo: nil, repeats: true) deleteButtonTimer!.tolerance = 0.01 NSRunLoop.mainRunLoop().addTimer(deleteButtonTimer!, forMode: NSDefaultRunLoopMode) } default: print("timer not added as expected") deleteButtonTimer?.invalidate() deleteButtonTimer = nil } }

It works perfectly in iOS simulator: when long press the Delete Button "long press confirmed" get printed once only and the code addTimer() get executed and deletion continously executed as expected. But when I run in iPhone, system print "long press confirmed" once and then continuously print "timer not added as expected". It seems NSRunLoop.mainRunLoop().addTimer(deleteButtonTimer!, forMode: NSDefaultRunLoopMode) is not executed.

Why UILongPressGestureRecognizer work in iOS simulator but not work for iPhone.

BTW I am using Xcode 7.2 and iPhone6S iOS9.2.

最满意答案

当您陷入default情况时, gestureRecognizer.state的值是gestureRecognizer.state ?

最有可能的是,在手机上,您的手指稍微移动,导致手势识别器更改为状态UIGestureRecognizerState.Changed 。 发生这种情况时,您不希望使计时器无效。

What is the value of gestureRecognizer.state when you fall into the default case?

Most likely, on the phone, your finger is moving slightly, causing the gesture recognizer to change to state UIGestureRecognizerState.Changed. You don't want to invalidate your timer when that happens.

更多推荐