Cancel UITouch Events When View Covered By Modal UIViewController

Posted by kkrizka on Stack Overflow See other posts from Stack Overflow or by kkrizka
Published on 2010-03-27T23:35:09Z Indexed on 2010/03/27 23:43 UTC
Read the original article Hit count: 546

Hi there,

I am writing an application where the user has to move some stuff on the screen using his fingers and drop them. To do this, I am using the touchesBegan,touchesEnded... function of each view that has to be moved.

The problem is that sometimes the views are covered by a view displayed using the [UIViewController presentModalViewController] function. As soon as that happens, the UIView that I was moving stops receiving the touch events, since it was covered up. But there is no event telling me that it stopped receiving the events, so I can reset the state of the moved view.

The following is an example that demonstrates this. The functions are part of a UIView that is being shown in the main window. It listens to touch events and when I drag the finger for some distance, it presents a modal view that covers everything. In the Run Log, it prints what touch events are received.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  NSLog(@"touchesBegan");

  touchStart=[[touches anyObject] locationInView:self];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  CGPoint touchAt=[[touches anyObject] locationInView:self];
  float xx=(touchAt.x-touchStart.x)*(touchAt.x-touchStart.x);
  float yy=(touchAt.y-touchStart.y)*(touchAt.y-touchStart.y);
  float rr=xx+yy;

  NSLog(@"touchesMoved %f",rr);
  if(rr > 100) {
    NSLog(@"Show modal");
    [viewController presentModalViewController:[UIViewController new] animated:NO];
  }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  NSLog(@"touchesEnded");
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
  NSLog(@"touchesCancelled");
}

But when I test the application and trigger the modal dialog to be displayed, the following is the output in the Run Log.

[Session started at 2010-03-27 16:17:14 -0700.] 2010-03-27 16:17:18.831 modelTouchCancel[2594:207] touchesBegan 2010-03-27 16:17:19.485 modelTouchCancel[2594:207] touchesMoved 2.000000 2010-03-27 16:17:19.504 modelTouchCancel[2594:207] touchesMoved 4.000000 2010-03-27 16:17:19.523 modelTouchCancel[2594:207] touchesMoved 16.000000 2010-03-27 16:17:19.538 modelTouchCancel[2594:207] touchesMoved 26.000000 2010-03-27 16:17:19.596 modelTouchCancel[2594:207] touchesMoved 68.000000 2010-03-27 16:17:19.624 modelTouchCancel[2594:207] touchesMoved 85.000000 2010-03-27 16:17:19.640 modelTouchCancel[2594:207] touchesMoved 125.000000 2010-03-27 16:17:19.641 modelTouchCancel[2594:207] Show modal

Any suggestions on how to reset the state of a UIView when its touch events are interrupted by a modal view?

© Stack Overflow or respective owner

Related posts about uitouch

Related posts about iphone