当视图已经被按下时如何响应触摸?

How to respond to touches when the view is already pressed?

我在 ViewController 中有两个视图,它们在按下时执行特定的操作。如果我用一根手指按住其中一个并用另一根手指触摸相同的视图,则什么也不会发生。下面的 "ok" 测试没有出现。

我覆盖方法touchesBegan来执行操作:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    print("ok")
    if let touch = touches.first{
        let viewTag = touch.view!.tag
        if viewTag == 101 {
            // my action for view 1
        } else if viewTag == 102 {
            // my action for view 2
        }
    }
    super.touchesBegan(touches, withEvent: event)
}

编辑

我已经在使用 multipleTouchEnabled = true

您必须在视图上启用多点触控:

self.view.multipleTouchEnabled = true;

来自文档:

multipleTouchEnabled

A Boolean value that indicates whether the receiver handles multi-touch events.

When set to YES, the receiver receives all touches associated with a multi-touch sequence. When set to NO, the receiver receives only the first touch event in a multi-touch sequence. The default value of this property is NO.

Other views in the same window can still receive touch events when this property is NO. If you want this view to handle multi-touch events exclusively, set the values of both this property and the exclusiveTouch property to YES.

必须在两个视图中都设置 multipleTouchEnabled = true,而不仅仅是主视图。

将此代码放入 viewDidLoad:

let tags = [101, 102]
for v in view.subviews {
    if tags.contains(v.tag) {
        v.multipleTouchEnabled = true
    }
}