How do I handle an event when a user clicks outside of a UIView?

I have a custom popup menu in an iOS app. This is a UIView with buttons on it. How to handle an event when the user goes beyond this view? I want to hide the menu at this moment.

+3
source share
4 answers

You must create a custom UIButtonone that will handle the entire screen. Then add your preview on top of this button. Then, when the user hits beyond the subtitle, they will click the button.

For example, make a button as follows:

UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(yourhidemethod:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"" forState:UIControlStateNormal];
button.frame = self.view.frame;
[self.view addSubview:button];

(where yourhidemethod:is the name of the method that removes your subview.) Then add your subview on top of it.


. , , , . :

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{   
  UITouch *touch = [touches anyObject];
  CGPoint touchPoint = [touch locationInView:self]; //location is relative to the current view
  // do something with the touched point 
}
+9

.

+3

, (uicontrol), , , .

+1

, , UIControl, UIView. , , , - UIControlEventTouchUpOutside. , .

+1

All Articles