ios - 错误的 UILongPressGestureRecognizer IBAction 被解雇

标签 ios swift uilongpressgesturerecogni

我在 IB 中创建了两个长按手势识别器,并为它们创建了两个 IBAction。

@IBAction func longPressGesture(_ gesture: UILongPressGestureRecognizer) {
    print("Do long press")

}


@IBAction func longPressTapGesture(_ gesture: UILongPressGestureRecognizer) {
    print("Do something else")

}

longPressGesture 设置为 > 0.5,0 次点击,1 次触摸。

longPressTapGesture 设置为 > 0.5,1 次点击,1 次触摸。

所以从技术上讲,当我启动应用程序并按下屏幕上的任意位置时,我应该触发 longPressGesture。

相反,在我开始运行该应用程序后的第一次,它总是触发 longPressTapGesture,无论我使用什么手势。

如果我抬起手指,然后再次按下,这次会触发 longPressGesture。

为什么 longPressTapGesture 会触发,即使我只是长按一次,有什么建议吗?

谢谢。

最佳答案

@Bevan,根据我所做的测试,您没有做错任何事。通过 Storyboard添加手势识别器时似乎出现了问题。当我使用 Storyboard构建测试应用程序并设置您在上面描述的设置时,我看到了您所看到的确切问题。但是,当我在代码中创建相同的设置时,行为会如您所期望的那样起作用。对于这种情况,最好的选择可能是使用代码而不是 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")

    }
}

关于ios - 错误的 UILongPressGestureRecognizer IBAction 被解雇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48974212/

相关文章:

ios - 带有 HeaderView 的 UITableView 不滚动

swift - 如何从选择器中获取值(或索引)并将其与实体属性的值进行比较?

swift - 使用可选类型时的 RealmSwift 和 Codable

ios - 对长按手势执行多项操作

swift - 尝试调用长按手势并运行命令,但在函数中使用选择器时遇到 SIGABRT 错误。什么没有连接?

ios - 如何在 Objective C 中将 NSLog 分类为错误、警告、消息和故障类型

ios - 如何在我滚动时为 UICollectionview 单元格设置动画

ios - ionic 4 : The "src" content of an "img" tag is not updating when I change it from the controller in IOS

ios - 使用Search Controller执行搜索后退出编辑状态

ios - 如何截取uiview的屏幕截图并快速拖动?