开发者

How do I handle event when user touch up outside the UIView?

开发者 https://www.devze.com 2023-02-15 21:23 出处:网络
I have a custom popup menu in my iOS application. It is UIView with buttons on it. How do I handle event when user touch up outside开发者_Go百科 this view? I want to hide menu at this moment. You shou

I have a custom popup menu in my iOS application. It is UIView with buttons on it. How do I handle event when user touch up outside开发者_Go百科 this view? I want to hide menu at this moment.


You should create a custom UIButton that takes up the whole screen. Then add your subview on top of that button. Then when the user taps outside of the subview they will be tapping the button.

For example, make the button like this:

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.


Update: It looks like you're wanting to know how to detect where touches are in a view. Here's what you do:

-(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 
}


Put it in full-screen transparent view and handle touches for it.


One idea is to have an invisible view(uicontrol) that is as big as the screen which then holds this custom popup.


It sounds like you'd be better served by deriving your menu from UIControl rather than UIView. That would simplify the touch handling, and all you'd have to do in this case would be to set a target and action for UIControlEventTouchUpOutside. The target could be the menu itself, and the action would hide or dismiss the menu.

0

精彩评论

暂无评论...
验证码 换一张
取 消