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

标签 iphone objective-c ios sdk

我的 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/

相关文章:

iphone - "cellForRowAtIndexPath:"中 indexPath.section 的奇怪行为

objective-c - 获取 Finder 前窗的 POSIX 路径

iphone - 我们打算支持iPhone 5,是否需要将Xcode升级到v4.5并使用iOS 6 SDK?

ios - 如何禁用 tableView 滚动?

ios - 禁用 UIPageViewController 反弹

iphone - 如何从 plist 文件中提取字符串进行翻译(本地化)?

iphone - 移动时 PickerView 崩溃 EXC_BAD_ACCESS

ios - 单元测试按钮单击不起作用

ios - 如何在我的应用程序 ios 10 中打开默认音乐应用程序

iOS 在 Firebase Crashlytics 仪表板中加载问题时出错