Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you force a touch event to cancel after touchesBegan?

Given a certain state of one of my UIViews I'd like it to respond to touchesBegan and then cancel any further touchesMoved/Ended that continue from that particular touchesBegan event.

Basically I have an UIView subclass at rest and if it's touched I'd like to move some other UIViews out of the way... then the next time it's touched the responder chain can respond normally to touchesMoved/Ended...

yes, i could check for this state in touchesMoved/Ended but I'm wondering if there's a way to just derail that touch event mid-phase.

like image 387
Meltemi Avatar asked Jul 23 '09 06:07

Meltemi


2 Answers

    let fakeTouch:Set<NSObject> = [UITouch()]
    let event     = UIEvent()

    self.touchesEnded(fakeTouch, withEvent: event)
like image 139
Who Is The Baddest Avatar answered Oct 29 '22 13:10

Who Is The Baddest


Remove the view for the view hierarchy

This is a very old post without a convincing answer, so I decided to add my workaround

Working with Xcode 9, Swift 4 and iOS 11, these solutions don't interrupt the touch

self.resignFirstResponder()
self.touchesEnded(touches, with: event)

The work around I found is the following:

let superview = self.superview
self.removeFromSuperview()
superview?.addSubview(self)

This is easy to implement in code only projects. I did not test it using views in Storyboard.

You would need to make sure you manage the view hierarchy, in case you can to restore o the view in the same point in the subviews stack. I assign tags to views to manage the position in the superview.subviews array

like image 1
eharo2 Avatar answered Oct 29 '22 15:10

eharo2