Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to discard touch events?

Tags:

ios

I have a iOS game and during loading screens any touches the user does seem to be buffered up, so once the loading it done (it can take a few seconds), I get the touch events.

Is there way for me to discard all touches?

like image 395
Roger Gilbrat Avatar asked Dec 03 '25 16:12

Roger Gilbrat


1 Answers

Ok this is a really old question but for anyone stumbling upon this can simply follow either of the below two approaches:

Approach 1.

DiscardTouchView.addGestureRecognizer(UITapGestureRecognizer())

Basically adding an empty gesture. So on tapping that view nothing happens.

Approach 2.

In this case you don't add empty gesture recognizer to the view. In case DiscardTouchView is a subview of SomeParentView which has a UIGestureRecognizer object, you can get that object and ignore it.

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let tappedView = touches.first?.view else { return }
    if(tappedView == DiscardTouchView) {
        guard let recognizer = touches.first?.gestureRecognizers?.first else { return }
        recognizer.ignore(touches.first!, for: event!)
    }
}

Setting userInteractionEnabled to false actually passes the touch event from subview to superview. It doesn't discard the touch event.

Setting userInteractionEnabled to true for a simple UIView does the same.

like image 135
Rishab Avatar answered Dec 06 '25 08:12

Rishab