Ios 错误的UILongPressGestureRecognitor iAction已启动
我在IB中创建了两个长按手势识别器,并为它们创建了两个IBActionIos 错误的UILongPressGestureRecognitor iAction已启动,ios,swift,uilongpressgesturerecogni,Ios,Swift,Uilongpressgesturerecogni,我在IB中创建了两个长按手势识别器,并为它们创建了两个IBAction @IBAction func longPressGesture(_ gesture: UILongPressGestureRecognizer) { print("Do long press") } @IBAction func longPressTapGesture(_ gesture: UILongPressGestureRecognizer) { print("Do something else"
@IBAction func longPressGesture(_ gesture: UILongPressGestureRecognizer) {
print("Do long press")
}
@IBAction func longPressTapGesture(_ gesture: UILongPressGestureRecognizer) {
print("Do something else")
}
长按手势设置为>0.5、0次轻触、1次触摸
LongPress tap手势设置为>0.5,轻触1次,轻触1次
所以从技术上讲,当我启动应用程序并按下屏幕上的任意位置时,我应该让LongPress手势启动
相反,在我开始运行应用程序后的第一次,无论我使用什么手势,都是长按Tap手势触发
若我抬起手指,然后再次按下,此时长按手势将触发
有什么建议可以解释为什么我只是长按一次,但长按TapPassive仍会启动
谢谢。@Bevan,根据我做的测试,你没有做错什么。在通过故事板添加手势识别器时,似乎出现了问题。当我使用storyboard构建测试应用程序并设置您上面描述的设置时,我看到了您看到的确切问题。但是,当我在代码中创建相同的设置时,行为函数与您预期的一样。在这种情况下,最好使用代码而不是故事板 代码示例如下:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let tap = UILongPressGestureRecognizer(target: self, action: #selector(longPressTapGesture))
tap.minimumPressDuration = 0.5
tap.numberOfTapsRequired = 1
tap.numberOfTouchesRequired = 1
view.addGestureRecognizer(tap)
let press = UILongPressGestureRecognizer(target: self, action: #selector(longPressGesture))
press.minimumPressDuration = 0.5
press.numberOfTouchesRequired = 1
view.addGestureRecognizer(press)
}
@objc func longPressGesture(_ gesture: UILongPressGestureRecognizer) {
print("Do long press")
}
@objc func longPressTapGesture(_ gesture: UILongPressGestureRecognizer) {
print("Do something else")
}
}
调用该函数时,手势识别器处于什么状态?UILongPressGestureRecognitor有两种状态,开始和结束。尝试以下操作:@IBAction func longTap_uuSender:UIgestureRecognitizer{printLong tap if sender.state==.End{printuiegestureRecognitizerStateEnd//在手势结束时做任何你想做的事}否则如果sender.state=.start{printuiegestureRecognitizerStateStart//在手势开始时做任何你想做的事}不管它处于什么状态。调用了错误的函数。谢谢@Steve!我以为我疯了!我以前在代码中有过它们,但我喜欢IB的干净性和一致性,我可以像按钮一样用iAction设置它们,所以我改变了它。我怀疑这是一个bug,但看起来我必须重新使用代码了!非常感谢您的确认和示例代码!没问题,很高兴能帮上忙。你知道我如何实现拖动滚动吗?i、 e.轻触屏幕,同时按住,向上或向下拖动,使文本视图的内容向上或向下滚动?如果您使用IB实现一半和一半,另一个使用代码,也可以使用此功能。一定是Xcode IB中的一个bug。对于拖拉问题,我肯定认为这是一个IB bug。您是否询问在文本视图中滚动文本?如果textview的框架小于内容的大小,则这将是textview的默认行为。