1

I have a view that I am showing. The view appears on the same screen with UITableView. I want to dismiss the view when anywhere on the screen is touched, but I don't want to prevent that touch from getting to its target. For instance, if the user goes to scroll the table view or select a cell or anything - I want to know so I can dismiss the view but I want whatever the user was touching to react as it normally would. Any help would be great.

Brian
  • 3,571
  • 7
  • 44
  • 70

3 Answers3

1

Implement hitTest:withEvent: method on that UIView's class. Check the location of the touch. If your view's frame does not contain it - dismiss the view.

Eimantas
  • 48,927
  • 17
  • 132
  • 168
1

Hope it helps:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
  UITouch *touch = [[event allTouches] anyObject];

 CGPoint location = [touch locationInView: touch.view];
  if(CGRectContainsPoint(tableView.frame, location) || CGRectContainsPoint(anyOtherObject.frame, location))
 {
    [self performAction];
 }
[self.view removeFromSuperView];
}
Deviator
  • 688
  • 3
  • 7
0

If you use a UIPopover or UIActionSheet, it will exhibit this behavior almost exactly. I have done this on a UITableView and a UITableView embedded in a UIView.

DogEatDog
  • 2,899
  • 2
  • 36
  • 65