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 o
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;
}
This code will help you. I disable touch event in touchesMoved:
method and I am enabling touch event in touchesCancelled:
method
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
.......
.......
Your code
.......
.......
//Condition when you want to disable touch event
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
.......
.......
Your code
.......
.......
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}
I've faced the same problem recently and found a standard way to solve it. You can use [[UIApplication sharedApplication] beginIgnoringInteractionEvents] to stop delivering touchesMoved events to your whole app. Make sure to enable them using [[UIApplication sharedApplication] endIgnoringInteractionEvents] when you need to receive touches again.
I don't think it's possible because I don't see it documented and none of these solutions work.
On iOS5 there seems to be a private method in UITouch
-(void)setSentTouchesEnded:(BOOL)ended;
Depending on apple's implementation it could stop sending events
Another way of doing so would be using associative objects
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([(NSNumber *)objc_getAssociatedObject(touch, &outKey) boolValue])
return;
if (sometouchisoutsideofview) {
objc_setAssociatedObject(touch, &outKey, [NSNumber numberWithBool:YES], OBJC_ASSOCIATION_RETAIN);
}
}
You need to call [super touchesMoved:withEvent:]
in order to let the super view clean up from the event but more importantly, you need to not call [super touchesCancelled:withEvent:]
.
Here's what I used on a cell to keep it from getting selected when I detected a swipe:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!showingEdit) {
if (IS_FAR_ENOUGH_TO_BE_A_SWIPE) {
RESPOND_TO_SWIPE
showingEdit = YES;
[super touchesCancelled:touches withEvent:event];
} else {
[super touchesMoved:touches withEvent:event];
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!showingEdit) {
[super touchesEnded:touches withEvent:event];
}
}