zoukankan      html  css  js  c++  java
  • UI基础:事件.响应链 分类: iOS学习-UI 2015-07-03 19:51 1人阅读 评论(0) 收藏

    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

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Android开源图表图形库K线图
    交易所8种作死方式
    Android百大框架排行榜
    15类Android通用流行框架
    如何避免Scrum敏捷开发团队反思会形式化,海星法介绍
    2018年Android面试题含答案--适合中高级
    FCoin优势
    golang学习笔记16 beego orm 数据库操作
    金融系统中加密机的简介
    ESB(Enterprise Service Bus)企业服务总线介绍
  • 原文地址:https://www.cnblogs.com/shaoting/p/4619805.html
Copyright © 2011-2022 走看看