当用户在UIView外部触摸时如何处理事件? [英] How do I handle event when user touch up outside the UIView?

查看:40
本文介绍了当用户在UIView外部触摸时如何处理事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iOS应用程序中有一个自定义弹出菜单.它是带有按钮的UIView.当用户在此视图外触摸时如何处理事件?我现在想隐藏菜单.

I have a custom popup menu in my iOS application. It is UIView with buttons on it. How do I handle event when user touch up outside this view? I want to hide menu at this moment.

推荐答案

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

You should create a custom UIButton that takes up the whole screen. Then add your subview on top of that button. Then when the user taps outside of the subview they will be tapping the button.

例如,使按钮如下:

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:是删除子视图的方法的名称.)然后在其顶部添加子视图.

(where yourhidemethod: is the name of the method that removes your subview.) Then add your subview on top of it.

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

Update: It looks like you're wanting to know how to detect where touches are in a view. Here's what you do:

-(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 
}

这篇关于当用户在UIView外部触摸时如何处理事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆