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开始一层一层往外递交,该过程中一旦有对象对事件作出处理,事件递交工作就会停止.但是如果整个过程都没有对象作出处理,整个事件就会被丢弃掉.