当用户在UIView外部触摸时如何处理事件?

Sia*_*sou 3 iphone sdk objective-c ios

我的iOS应用程序中有一个自定义弹出菜单.UIView上有按钮.当用户在此视图外部进行修饰时,如何处理事件?我想暂时隐藏菜单.

sud*_*-rf 9

您应该创建一个UIButton占据整个屏幕的自定义.然后在该按钮的顶部添加子视图.然后,当用户点击子视图之外时,他们将点击按钮.

例如,按下这样的按钮:

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];
Run Code Online (Sandbox Code Playgroud)

(yourhidemethod:删除子视图的方法的名称在哪里.)然后在其上添加子视图.


更新:您似乎想知道如何检测视图中的触摸位置.这是你做的:

-(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 
}
Run Code Online (Sandbox Code Playgroud)