使用 numberOfTouchesRequired 实现触控板的二次点击

Using numberOfTouchesRequired to implement secondary click with 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

但它只能由屏幕上的触摸触发。 使用触控板不会触发任何东西。 我已经将 UIApplicationSupportsIndirectInputEvents 设置为 YES/NO

我刚从这个网站找到了答案: 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