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

你说的曾经没有我的故事 提交于 2019-12-10 19:12:16

问题


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 this view? I want to hide menu at this moment.


回答1:


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 
}



回答2:


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




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/5263377/how-do-i-handle-event-when-user-touch-up-outside-the-uiview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!