msampaio
msampaio

Reputation: 3443

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

Upvotes: 0

Views: 162

Answers (3)

msampaio
msampaio

Reputation: 3443

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

Upvotes: 0

Teja Nandamuri
Teja Nandamuri

Reputation: 11201

From Documentation:

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.

Upvotes: 1

Diego
Diego

Reputation: 587

You have to enable multiple touches on your view:

self.view.multipleTouchEnabled = true;

Upvotes: 1

Related Questions