Search code examples
iosswiftuiviewuitouchuievent

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


I have two views in a ViewController that perform specific actions when touched down. If I keep one of them pressed with one finger and touch the same view with another finger, nothing happens. The "ok" test below doesn't appear.

I override the method touchesBegan to perform the actions:

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)
}

Edit

I'm already using multipleTouchEnabled = true


Solution

  • The multipleTouchEnabled = true must be set in both views, not only the main one.

    Put this code in the viewDidLoad:

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