How to cancel a sequence of UITouch events?

subjective-c picture subjective-c · Feb 10, 2009 · Viewed 18.1k times · Source

I have a UIImage view that responds to touch events. I want to cancel the touch sequence, i.e., further calls to touchesMoved:, if the touch goes outside of certain bounds. How can I do that?

I know that in touchesMoved: I can inspect the coordinates of the touch object and ignore it, but what I don't know is how to cancel the sequence altogether. I don't see any method documented in the Apple Developer UIResponder Reference that I can call to cancel a touch sequence.

Answer

Michael Fey picture Michael Fey · Feb 10, 2009

This solution may be a bit kludgy, but you could implement and manually call

- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

I am basing this solution loosely on some tweaking I did to the MoveMe sample app on Apple's iPhone sample code site where I modified the touchesMoved method to look like this:

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
     UITouch *touch = [touches anyObject];
     if ([touch view] == placardView)
         CGPoint location = [touch locationInView:self];
         placardView.center = location;
         // I added the following line:
         [self touchesCancelled:touches withEvent:event];
         return;
}