首页 > 解决方案 > 使用 numberOfTouchesRequired 实现与 TrackPad 的二次点击

问题描述

我想在 iPad 上实现二次点击以显示选项。

let tapTwoRecog = UITapGestureRecognizer(target: self, action: #selector(tapTwoAsLongPress(gesture:)))
        tapTwoRecog.numberOfTouchesRequired = 2
        tapTwoRecog.numberOfTapsRequired = 1
        tapTwoRecog.allowedTouchTypes = [NSNumber(value: UITouch.TouchType.direct.rawValue), NSNumber(value: UITouch.TouchType.indirect.rawValue)]
        tableView.addGestureRecognizer(tapTwoRecog)
        tableView.isMultipleTouchEnabled = true

但它只能由屏幕上的触摸触发。使用触控板不会触发任何事情。我已经设置UIApplicationSupportsIndirectInputEventsYES/NO.

标签: iosswiftipados

解决方案


我刚刚从这个网站找到了答案: https ://pspdfkit.com/blog/2020/level-up-your-trackpad-support-using-uiinteraction/

if #available(iOS 13.4, *) {
    let tapTwoRecog = UITapGestureRecognizer(target: self, action: #selector(tapTwoAsLongPress(gesture:)))
    tapTwoRecog.allowedTouchTypes = [NSNumber(value: UITouch.TouchType.indirectPointer.rawValue)]
    tapTwoRecog.buttonMaskRequired = .secondary
    tableView.addGestureRecognizer(tapTwoRecog)
}

UIApplicationSupportsIndirectInputEvents必须YES是。


推荐阅读