I have a ViewController that defines touchesMoved() to increment a counter as a user drags a finger over the screen:
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
numMoved += 1
print("Moved: \(numMoved)")
}
When the user goes to another view controller which doesn't have touchesMoved() defined, I expect that nothing would happen. (The new view controller is defined to be of class OtherViewController, which inherits directly from UIViewController and so does not get the touchesMoved() override I had defined for the first view controller.)
However, after tapping a button that brings me to the other view controller, I still see the printed messages, meaning touchesMoved() is still being invoked even though I am no longer on the original view controller that is tracking the drag.
Am I missing some simple thing that would ensure that the override to touchesMoved() only affects the ViewController it is defined within?
Thanks for any insights.
Instead of defining touchesMoved
in a view controller, define it in a view. The main view of the view controller will do. (You will need to subclass UIView and make the main view of the view controller an instance of this subclass.)
It will have the same effect, but now you are not in the view controller hierarchy and you won't get messages from the other view controller's views.
Another alternative: pull a UITouch out of the touches:
and look at its view
. If this isn't your view, ignore this call.