如何让触摸事件穿透一个View
2014-10-23 21:24
344 查看
如何让触摸事件穿透一个View
偶然间发现,如何屏蔽或者让触摸事件穿透一个view是一个很简单的事情。
现象:
源码:
其实原理很简单,一个触摸事件时候被view接受或者屏蔽,完全由这个参数决定,那就是userInteractionEnabled。
所以,当你想屏蔽掉一个事件不让他穿透,设置覆盖的view的userInteractionEnabled为NO即可。
偶然间发现,如何屏蔽或者让触摸事件穿透一个view是一个很简单的事情。
现象:
源码:
// // ViewController.m // UserInteraction // // Created by YouXianMing on 14/10/23. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 新建按钮 UIButton *button = [[UIButton alloc] initWithFrame:self.view.bounds]; [button addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; // 覆盖在上层的view UIView *testView = [[UIView alloc] initWithFrame:self.view.bounds]; [self.view addSubview:testView]; testView.userInteractionEnabled = YES; } - (void)buttonEvent:(UIButton *)button { NSLog(@"触摸事件"); } @end
其实原理很简单,一个触摸事件时候被view接受或者屏蔽,完全由这个参数决定,那就是userInteractionEnabled。
所以,当你想屏蔽掉一个事件不让他穿透,设置覆盖的view的userInteractionEnabled为NO即可。
相关文章推荐
- 触摸事件的重新分发(当弹出一个新窗口时,如何屏蔽掉下面层的触摸事件)
- 一个view如何同时响应onTouch和onClick事件
- 如何让UILabel或UIImageView接受一个单击事件或者其他事件
- 两个view重叠时如何只让上层view响应触摸事件
- 如何为一个按钮(View)设置任意多次点击事件以及可以同时响应多个多次点击事件
- 两个view重叠时如何只让上层view响应触摸事件
- 如何让UILabel或UIImageView接受一个单击事件?
- 如何在S60 5th中模拟触摸事件
- 如何做一个按钮二个事件
- [VB.NET][问]如何让父窗体响应子窗体中的某一个自定义事件
- 一个usercontrol如何能从这个userctrl的控件如button上向组建所在窗体发出一个事件
- 如何在一个控件中使Tab键作为一般的输入键来触发KeyDown,KeyUp事件呢?
- 如何在一个FormView里获得另一个FormView的指针
- Silverlight制作一个简单的菜单1-用到的知识讲解 xml数据读取,事件如何传递参数
- 已知道 一个窗口的句柄, 如何扑捉该窗口的onresize 事件,并在onresize 事件中写入自己的代码?
- 如何在客户端写脚本触发一个服务器端事件处理过程.在线等.
- 如何做一个按钮二个事件
- 向大家请教一个关于sharepoint2003的问题,如何使一个Custom List in Datasheet View中某个field从其他list抽取数据当显示时?
- 如何让一个影片按钮区分开拖动和点击事件
- [VB.NET]如何在一个窗体M中判断另一个窗体N中是否存在一个名称为A的事件?