UI基础:事件.响应链

来源:互联网 发布:java调用python返回值 编辑:程序博客网 时间:2024/06/04 18:19

UIEvent:事件,是由硬件捕捉的一个代表用户操作操作设备的对象.
事件分三类:触摸事件.晃动事件.远程控制事件.

触摸事件:用户通过触摸设备屏幕操作对象,.输入数据.支持多点触摸,包含1个到多个触摸点.
UIView支持触摸事件(继承了UIResponder),而且支持多点触摸
使用时,需要定义UIView子类,重写触摸相关的方法.
1.刚开始触摸到屏幕的时候触发

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{}

2.触摸事件被意外中断的时候触发(如:来电)

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{    }

3.当手指离开屏幕的时候触发

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{}

4.当手指在视图上移动的时候触发

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{}

UITouch 点击类
touchs 存放手指对象
如:

1.当单击时UITouch *touch=[touches anyObject]; //获取手指对象的数组2.但双击时NSArray *allTouch=[touches allObjects];//获取手指对象UITouch *touch1=[allTouch firstObject];UITouch *touch2=[allTouch lastObject];

手势:有规律的触摸
UITouch代表触摸在屏幕上的一根手指,可以获取触摸时间和触摸位置.
获取touch对象:touches集合中包含了视图上的所有手势.


响应者链
有多个响应者对象组成的链.
UIResponder 响应者类
iOS中所有能响应事件(触摸.晃动.远程事件)的对象都是响应者.
系统定义了一个抽象的父类UIResponder来表示响应者.其子类都是响应者.

检测触碰视图
硬件检测到触摸操作,会将信息交给UIApplication,开始检测.
检测过程:
UIApplication -> window ->viewController ->view ->检测所有子视图
处理触摸事件:
事件处理的顺序与触摸检测查询相反.(自己的用户交互关闭,就交给父类处理)
触摸的子视图 -> view -> viewController ->window ->UIApplication

阻断响应者链
响应者链可以被打断.无法完成检测查询过程.
视图类的属性:userInteractionEnabled 关闭后可以阻断查询过程

__其中,下面是在事件里面几个简单程序的主要代码段
1.实现点击屏幕一次换颜色.点击两次换父视图的颜色

//当手指离开屏幕的时候触发-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"%s",__FUNCTION__);    //    UITouch  点击类    // touchs   存放手指对象    UITouch *touch=[touches anyObject];    if (1==touch.tapCount) {        //        当视图识别为单击,延迟执行之下的方法,看是否会有再次点击        [self performSelector:@selector(changeMyselfBackGroundColor) withObject:nil afterDelay:0.3];//        self.backgroundColor=[UIColor randomColor];    }else if(2==touch.tapCount){//       当识别为双击的时候,取消之前的操作        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(changeMyselfBackGroundColor) object:nil];        self.superview.backgroundColor=[UIColor randomColor];    }}-(void)changeMyselfBackGroundColor{    self.backgroundColor=[UIColor randomColor];}

其中,对UIcolor进行了类目,使其有个随机颜色的功能

@implementation UIColor (Random)+(UIColor *)randomColor{    return [UIColor colorWithRed:COLORVALUE green:COLORVALUE blue:COLORVALUE alpha:1.0];}@end

2.实现缩放视图的功能

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{    if (1==touches.count) {        return;    }else{        //    获取手指对象的数组        NSArray *allTouch=[touches allObjects];        //获取手指对象        UITouch *touch1=[allTouch firstObject];        UITouch *touch2=[allTouch lastObject];        //    获取两个手指的当前位置        CGPoint currenFirstPoint=[touch1 locationInView:self];        CGPoint currenSecondPoint=[touch2 locationInView:self];        //    获得两个手指当前的距离        CGFloat currentDistance=[self distanFromPoint:currenFirstPoint toPoint:currenSecondPoint];        //    获取之前两手指的位置        CGPoint previousFirstPoint=[touch1 previousLocationInView:self];        CGPoint previousSecondPoint=[touch2 previousLocationInView:self];        //    获取之前两手指之间的距离        CGFloat currentsDistance=[self distanFromPoint:previousFirstPoint toPoint:previousSecondPoint];        //    获取比例        CGFloat rate=currentDistance/currentsDistance;        //    缩放视图,中心点不变,修改bounds即可        self.bounds=CGRectMake(0, 0, self.frame.size.width*rate, self.frame.size.height*rate);    }}//封装计算两点之间的距离的方法-(CGFloat)distanFromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint{    CGFloat dx=fromPoint.x-toPoint.x;    CGFloat dy=fromPoint.y-toPoint.y;    return sqrt(dx*dx+dy*dy);}

3.实现视图随着手指移动

@implementation MoveView//触摸的方法没必要完全实现-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{    //先获取手指对象    UITouch *touch=[touches anyObject];//    获取自身视图手指当前的手指位置    CGPoint currenPoint=[touch locationInView:self];//    获取手指之前的位置    CGPoint perviousPoint=[touch previousLocationInView:self];//    计算移动的增量    CGFloat dx=currenPoint.x-perviousPoint.x;    CGFloat dy=currenPoint.y-perviousPoint.y;    CGPoint center=self.center;    self.center=CGPointMake(center.x+dx, center.y+dy);}@end
0 0