一直觉得很神秘,结果文章读完啦,还是不知道该怎么用,传递顺序见上篇
关于如何处理响应事件的处理,没有弄明白,姑且写个demo测验一下
-(void)loadView
{
self.view = [[NDTfirstView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view.backgroundColor = [UIColor blueColor];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
secondview = [[NDTSecondView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
secondview.backgroundColor = [UIColor redColor];
[self.view addSubview:secondview];
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[self.view addGestureRecognizer:gesture]; }-(void)tapAction:(UITapGestureRecognizer *)gesture
{
NSLog(@"tap Action");
}
如上图,事件添加在蓝色部分,点击红色部分 输出
2014-03-05 14:41:49.216 TouchTestView[2131:a0b] tap Action
点击蓝色部分,输出:2014-03-05 14:41:49.219 TouchTestView[2131:a0b] tap Action
结果分析:
红色部分没有捕获事件,蓝色部分捕获事件,所以,点击红色部分,事件同样会响应。
?如果点击红色部分,不响应,如何实现?
方法:
// called before touchesBegan:withEvent: is called on the gesture recognizer for a new touch. return NO to prevent the gesture recognizer from seeing this touch
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
示例代码:- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
// 输出点击的view的类名
NSLog(@"%@",NSStringFromClass([touch.viewclass]));
// 若为UITableViewCellContentView(即点击了tableViewCell),则不截获Touch事件
if ([NSStringFromClass([touch.viewclass])isEqualToString:@"UITableViewCellContentView"]) {
return NO;
}
原理:当hit-test view不是蓝色视图的时候,忽视touch事件
测验二:
添加代码:
secondview.userInteractionEnabled = NO;
点击红色部分:
2014-03-05 14:56:28.241 TouchTestView[2230:a0b] tap Action
结果分析:firstResponder 从hit-testView开始
点击蓝色部分:
同上响应,但是hit-testview都是firstview
userinterfaceEnabled:决定本视图的的responder是否从responderChain中移除,touch响应是否移除。 | |
Description | A Boolean value that determines whether user events are ignored and removed from the event queue. |
如果secondview的userinterface = NO;添加在secondview上的所有子视图的事件和响应也将移除(原因:隔离啦)
测验三:
添加代码
UITapGestureRecognizer *gesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap2Action:)];
[secondview addGestureRecognizer:gesture2];
-(void)tap2Action:(UITapGestureRecognizer *)gesture
{
NSLog(@"tap2 Action");
}
图片效果如测验一的图:
点击红色部分:
2014-03-05 14:51:10.512 TouchTestView[2131:a0b] tap2 Action
点击蓝色部分:
2014-03-05 14:51:48.077 TouchTestView[2131:a0b] tap Action
结果分析:
点击红色部分,红色视图捕获啦事件,事件不在向下传递。
小结:
补充:缘由:详情界面的处理逻辑 实际代码操作中
视图在添加的先后顺序影响传递的先后顺序,跟视图所在的上下没有关系
如果两个视图a,b,同时添加在c上,a----c、b---c的事件传递将没有任何关系;
验证略