【发布时间】:2011-03-10 17:25:56
【问题描述】:
我的 iOS 应用程序中有一个自定义弹出菜单。它是带有按钮的 UIView。当用户在此视图之外触摸时如何处理事件?我现在想隐藏菜单。
【问题讨论】:
标签: iphone objective-c ios sdk
我的 iOS 应用程序中有一个自定义弹出菜单。它是带有按钮的 UIView。当用户在此视图之外触摸时如何处理事件?我现在想隐藏菜单。
【问题讨论】:
标签: iphone objective-c ios sdk
您应该创建一个占据整个屏幕的自定义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
}
【讨论】:
将其置于全屏透明视图并为其处理触摸。
【讨论】:
UIButton 实例。之后,您只需将控制器(或用于处理此菜单的任何东西)添加为此按钮的目标,例如 [button addTarget:self action:@selector(transparenViewTapped:) forControlEvents:UIControlEventTouchUpInside]。
一个想法是有一个不可见的视图(uicontrol),它与屏幕一样大,然后保存这个自定义弹出窗口。
【讨论】:
听起来从 UIControl 而不是 UIView 派生菜单会更好。这将简化触摸处理,在这种情况下您所要做的就是为 UIControlEventTouchUpOutside 设置目标和操作。目标可以是菜单本身,操作会隐藏或关闭菜单。
【讨论】: