我的 iOS 应用程序中有一个自定义弹出菜单。它是带有按钮的 UIView。当用户在此 View 之外触摸时如何处理事件?我现在想隐藏菜单。
最佳答案
您应该创建一个占据整个屏幕的自定义 UIButton
。然后在该按钮的顶部添加您的 subview 。然后,当用户点击 subview 外部时,他们将点击按钮。
比如把按钮做成这样:
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];
(其中 yourhidemethod:
是删除 subview 的方法的名称。)然后在其上添加 subview 。
更新:您似乎想知道如何检测 View 中的触摸位置。以下是您的操作:
-(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
}
关于iphone - 当用户在 UIView 之外触摸时如何处理事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5263377/