什么是响应者链?
响应者链是一个响应者对象的连接序列,事件或动作消息 (或菜单编辑消息)依次传递。它允许响应者对象把事件 处理的职责转交给其它更高层的对象。应用程序通过向上 传递一个事件来查找合适的处理对象。因为点击检测视图 也是一个响应者对象,应用程序在处理触摸事件时也可以 利用响应链。 由多个响应者对象组成的链。
那组成响应者链的响应者又是哪些?
iOS中所有能响应事件(触摸、晃动、远程事件)的对象 都是响应者。系统定义了一个抽象的父类UIResponder来表示响应者。 其子类都是响应者。
检测过程:
硬件检测到触摸操作,会将信息交给UIApplication,开始检测。
UIApplication -> window -> viewController -> view -> 检测所有子 视图
最终确认触碰位置,完成响应者链的查询过程。
检测碰撞视图:
UIApplication->window- >rootViewController
viewA -> ViewB
viewC -> ViewD -> ViewE(检测到触摸 视图)
代码演示:
#import
"
RootViewController.h
"
@interface
RootViewController ()
@end
@implementation
RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//创建一个UIImageView
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(65, 65, 250, 250)];
imageView.backgroundColor = [UIColor greenColor];
//打开用户交互
imageView.userInteractionEnabled = YES;
//响应者链阻断之后,完成不了检测过程,检测过程完成不了,所以事件就触发不了
//而空间阻断响应者链就是关闭用户交互,默认关闭用户交互的控件有UIImageView和UILabel
//以后项目开发中,如果想让UIImageView和UILabel响应时间,必须将其交互打开 [self.view addSubview:imageView];
[imageView release];
//创建一个UIButton加到UIImageView上面
UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(50, 50, 150, 80);
[button setTitle:@"xxx" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//修改按钮显示文本的字体,先要找到titlelabel,按钮是个组合控件
button.titleLabel.font = [UIFont boldSystemFontOfSize:20];
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:button];
}
-(void)click{
NSLog(@"你好");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.}
@end
原文:http://www.cnblogs.com/16-jkd/p/5205815.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!