[英]How do I handle event when user touch up outside the UIView?
我的iOS应用程序中有一个自定义弹出菜单。 UIView上有按钮。 当用户在此视图外部进行修饰时,如何处理事件? 我想暂时隐藏菜单。
您应该创建一个占据整个屏幕的自定义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];
(其中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
}
将它放在全屏透明视图中并处理它的触摸。
一个想法是有一个不可见的视图(uicontrol),它与屏幕一样大,然后保存这个自定义弹出窗口。
听起来你可以通过从UIControl而不是UIView获取菜单来获得更好的服务。 这将简化触摸处理,在这种情况下您需要做的就是为UIControlEventTouchUpOutside设置目标和操作。 目标可以是菜单本身,操作将隐藏或关闭菜单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.