UI____UIEvent以及响应者链
2015-07-27 09:59
537 查看
UIEvent:事件,是由硬件捕捉的一个表示用户操作设备的对象:触摸事件;晃动事件;远程控制事件(耳机线控)
如果想要视图能够响应用户触摸,就必须使用UIView的子类.(为什么不直接使用UIView,因为我们拿不到UIView的.m文件,无法对响应做出反应).
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
为什么使用NSSet而不使用NSArray.因为我们也不知道哪个手指会先触摸,而集合刚好是无序的,并且因为集合是无序的,所以遍历起来效率很高
UITouch对应的就是手指类.
//移动触摸:在移动过程中,手指对象不能离开屏幕,同时该方法会被重复调用
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
//将要离开屏幕的时候触发
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
//取消触摸,,当触摸被意外打断的时候触发,比如来电话短信
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
__________________________________________________________________________________________________________________________________________
-----------------------------------------------------------------------------------------------------------------响应者链---------------------------------------------------------------------------------------------------------
回收键盘神器:
[self.view endEditing:YES];//原理:将自带View上面的所有子视图都取消第一响应者,所以自己上面的子视图textField也随之取消第一响应者.
这个方法要写在视图控制器里面(因为目的是获得视图控制器自带的视图)
这个视图的布局:
- (void)viewDidLoad {
[superviewDidLoad];
Custom *redView = [[Customalloc]initWithFrame:CGRectMake(0,0,
self.view.frame.size.width,self.view.frame.size.height/2)];
[self.viewaddSubview:redView];
redView.tag =100;
redView.backgroundColor = [UIColorredColor];
[redViewrelease];
Custom *yellowView = [[Customalloc]initWithFrame:CGRectMake(0,self.view.frame.size.height/2,self.view.frame.size.width,self.view.frame.size.height/2)];
[self.viewaddSubview:yellowView];
yellowView.tag =101;
yellowView.backgroundColor = [UIColoryellowColor];
yellowView.userInteractionEnabled =NO;
[yellowViewrelease];
Custom *greenView = [[Customalloc]initWithFrame:CGRectMake(DISTANCE,DISTANCE,
yellowView.frame.size.width -DISTANCE*2, yellowView.frame.size.height
-DISTANCE*2)];
[yellowViewaddSubview:greenView];
greenView.backgroundColor = [UIColorgreenColor];
greenView.tag =102;
greenView.userInteractionEnabled =NO;
[greenViewrelease];
Custom *blueView = [[Customalloc]initWithFrame:CGRectMake(DISTANCE,DISTANCE,
greenView.frame.size.width -DISTANCE*2, greenView.frame.size.height/2)];
blueView.backgroundColor = [UIColorblueColor];
blueView.tag =103;
blueView.userInteractionEnabled =NO;
[greenViewaddSubview:blueView];
[blueViewrelease];
}
对于事件的处理过程,分别是碰撞检测阶段和事件处理阶段.碰撞检测阶段产生了一个响应者链.
视图的用户交互默认是开启的,如果将视图的用户交互关闭,则视图以及碰撞检测过程中视图和它上面的子视图都无法检测到碰撞事件.
注意 UIlabel和UIImageVIew的用户交互默认是关闭的.其余的用户交互默认都是开启的.
事件处理:UIApplication --->AppDelegate--->window--->RootViewController --->self.View(根视图控制器对应的视图)
--->yellow--->green--->blue
事件处理的递交过程:事件从blueView开始一层一层往外递交,该过程中一旦有对象对事件作出处理,事件递交工作就会停止.但是如果整个过程都没有对象作出处理,整个事件就会被丢弃掉.
将yellowView的用户交互关掉,则碰撞检测事件在self.View终止,但是事件是被rootViewController作出响应.(因为self.view是一个UIView类型的对象,但是我们拿不到UIView 的.m文件)
如果想要视图能够响应用户触摸,就必须使用UIView的子类.(为什么不直接使用UIView,因为我们拿不到UIView的.m文件,无法对响应做出反应).
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
为什么使用NSSet而不使用NSArray.因为我们也不知道哪个手指会先触摸,而集合刚好是无序的,并且因为集合是无序的,所以遍历起来效率很高
UITouch对应的就是手指类.
//移动触摸:在移动过程中,手指对象不能离开屏幕,同时该方法会被重复调用
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
//将要离开屏幕的时候触发
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
//取消触摸,,当触摸被意外打断的时候触发,比如来电话短信
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
__________________________________________________________________________________________________________________________________________
-----------------------------------------------------------------------------------------------------------------响应者链---------------------------------------------------------------------------------------------------------
回收键盘神器:
[self.view endEditing:YES];//原理:将自带View上面的所有子视图都取消第一响应者,所以自己上面的子视图textField也随之取消第一响应者.
这个方法要写在视图控制器里面(因为目的是获得视图控制器自带的视图)
这个视图的布局:
- (void)viewDidLoad {
[superviewDidLoad];
Custom *redView = [[Customalloc]initWithFrame:CGRectMake(0,0,
self.view.frame.size.width,self.view.frame.size.height/2)];
[self.viewaddSubview:redView];
redView.tag =100;
redView.backgroundColor = [UIColorredColor];
[redViewrelease];
Custom *yellowView = [[Customalloc]initWithFrame:CGRectMake(0,self.view.frame.size.height/2,self.view.frame.size.width,self.view.frame.size.height/2)];
[self.viewaddSubview:yellowView];
yellowView.tag =101;
yellowView.backgroundColor = [UIColoryellowColor];
yellowView.userInteractionEnabled =NO;
[yellowViewrelease];
Custom *greenView = [[Customalloc]initWithFrame:CGRectMake(DISTANCE,DISTANCE,
yellowView.frame.size.width -DISTANCE*2, yellowView.frame.size.height
-DISTANCE*2)];
[yellowViewaddSubview:greenView];
greenView.backgroundColor = [UIColorgreenColor];
greenView.tag =102;
greenView.userInteractionEnabled =NO;
[greenViewrelease];
Custom *blueView = [[Customalloc]initWithFrame:CGRectMake(DISTANCE,DISTANCE,
greenView.frame.size.width -DISTANCE*2, greenView.frame.size.height/2)];
blueView.backgroundColor = [UIColorblueColor];
blueView.tag =103;
blueView.userInteractionEnabled =NO;
[greenViewaddSubview:blueView];
[blueViewrelease];
}
对于事件的处理过程,分别是碰撞检测阶段和事件处理阶段.碰撞检测阶段产生了一个响应者链.
视图的用户交互默认是开启的,如果将视图的用户交互关闭,则视图以及碰撞检测过程中视图和它上面的子视图都无法检测到碰撞事件.
注意 UIlabel和UIImageVIew的用户交互默认是关闭的.其余的用户交互默认都是开启的.
事件处理:UIApplication --->AppDelegate--->window--->RootViewController --->self.View(根视图控制器对应的视图)
--->yellow--->green--->blue
事件处理的递交过程:事件从blueView开始一层一层往外递交,该过程中一旦有对象对事件作出处理,事件递交工作就会停止.但是如果整个过程都没有对象作出处理,整个事件就会被丢弃掉.
将yellowView的用户交互关掉,则碰撞检测事件在self.View终止,但是事件是被rootViewController作出响应.(因为self.view是一个UIView类型的对象,但是我们拿不到UIView 的.m文件)
相关文章推荐
- POJ2031,Building a Space Station,最小生成树模版题
- hdu 5312 Sequence(三角形数)
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
- GUICE
- iOS—UI-手势及控件(segment switch slider)的使用
- 使用Hibernate SQLQuery执行原生SQL
- HDOJ 1005 Number Sequence(规律)
- Unique Attack (zoj 2587 判定最小割是否唯一)
- HDU 5311 Sequence
- 源码推荐(7.27):仿城觅2.0 附博客讲解及超全注释,聊天UI
- UIView、UILabel
- UEditor扩展上传
- H - Can you answer these queries? - (区间查询更新)
- Android MenuItem 设置文本颜色-TextColor设置
- Leetcode[232] Implement Queue using Stacks
- request_response详解
- [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列
- 三十而立,从零开始学ios开发(十二):Table Views(中)UITableViewCell定制 - minglz - 博客园
- easyUI校验封装
- 设置UIButton,UITextFild边框圆角(上半边或下半边)