UI学习笔记---第四天

事件处理

事件概述

UIEvent:事件,是由硬件捕捉的一个表示用户操作设备的对象

分三类:触摸事件\  晃动事件\ 远程控制事件

触摸事件:会包含1个到多个触摸点

 实现触摸

UIView支持触摸事件(因为继承于UIResponder),而且支持多点触摸

需要定义UIView子类,实现触摸相关的方法

touches..begin     touches..moved    touches..ended     touches..canceled

使用触摸实现手势

手势:有规律的触摸

UITouch代表触摸在屏幕上的一根手指,可以获取触摸时间和触摸位置

如何获取touch对象,touches集合中包含了视图上的所有手势

实现触摸事件

//建工程,添加根视图控制器,根视图控制器中添加TouchView类,创建一个视图对象

//TouchView.m中代码



#import "TouchView.h"



@implementation TouchView

-(void)dealloc

{

    [super dealloc];

}

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

{

//    NSLog(@"%s  %d",__FUNCTION__,__LINE__);

//    NSLog(@"开始");

//    //获得手指(触摸)对象,UITouch类在MVC中属于M,用于存储数据,并提供了一些方法,获取这些存储的数据,存储的数据很多,例如:点击时间,点击次数,点击的位置,上一次点击位置等等,详情见UITouch API.

//    UITouch *touch = [touches anyObject];

//    

//    //获取 手指  在 指定视图  中得位置

//    CGPoint currentPoint = [touch locationInView:self.superview];

//    self.center = currentPoint;

//    //把CGPoint转换为NSString  并打印出来

//    NSLog(@"%@",NSStringFromCGPoint(currentPoint));

    

//    NSLog(@"%@",touch);

}

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

{

    NSLog(@"%s  %d",__FUNCTION__,__LINE__);

    NSLog(@"移动");

    //获得手指(触摸)对象,UITouch类在MVC中属于M,用于存储数据,并提供了一些方法,获取这些存储的数据,存储的数据很多,例如:点击时间,点击次数,点击的位置,上一次点击位置等等,详情见UITouch API.

    UITouch *touch = [touches anyObject];

    

    //获取 手指  在 指定视图  中得位置

    CGPoint currentPoint = [touch locationInView:self.superview];

    //获取手指在指定视图的上一个位置

    CGPoint previousPoint = [touch previousLocationInView:self.superview];

    //计算偏移点

    CGPoint point = CGPointMake(currentPoint.x - previousPoint.x, currentPoint.y - previousPoint.y);

    //center  在偏移后的center值

    self.center = CGPointMake(self.center.x+point.x, self.center.y + point.y);

    [self.superview bringSubviewToFront:self];

//    self.center = point2;

}

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

{

    NSLog(@"%s  %d",__FUNCTION__,__LINE__);

    NSLog(@"取消");

}

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

{

    CGFloat red = arc4random()% 256 / 255.0;  //是float类型,除法运算需要加0

    CGFloat green = arc4random() % 256 / 255.0;

    CGFloat blue = arc4random() % 256 /255.0;

    NSInteger width = self.frame.size.width;

    NSInteger height = self.frame.size.height;

    CGFloat x = arc4random()%(320 - width +1) + width/2;//center的x坐标随机

    CGFloat y = arc4random()%(480 - width +1) + height/2;//center的y坐标随机



    

    NSLog(@"%s  %d",__FUNCTION__,__LINE__);

    NSLog(@"结束");

    self.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];

    self.center = CGPointMake(x, y);

    

    

}



- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

    }

    return self;

}



/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

    // Drawing code

}

*/



@end

轻扫事件

//SwipeView.m中代码

#import "SwipeView.h"



@implementation SwipeView

-(void)dealloc

{

    [super dealloc];

}

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        //设置支持多点触摸.默认是不支持的

        self.multipleTouchEnabled = YES;

        // Initialization code

    }

    return self;

}

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

{

    NSLog(@"%@",touches);

    UITouch *touch = [touches anyObject];

    _began = [touch locationInView:self];

    

}

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

{

    UITouch *touch = [touches anyObject];

    _end = [touch locationInView:self];

    CGFloat dx = _end.x - self.began.x;

    CGFloat dy = _end.y - self.began.y;

    if (dx>30 && fabs(dy)<10) {

        NSLog(@"向右轻扫");

        self.superview.backgroundColor = [UIColor redColor];

    }else if (dx<-30 && fabs(dy)<10)

    {

        NSLog(@"向左轻扫");

     self.superview.backgroundColor = [UIColor orangeColor];

    }

}

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

{



}

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

{



}

/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

    // Drawing code

}

*/



@end

响应者链

由多个响应者对象组成的链

UIResponder---响应者类

iOS中所有能响应事件(触摸  晃动 . 远程事件)的对象都是响应者

系统定义了一个抽象的父类UIResponder来表示响应者.其子类都是响应者

检测触碰视图

硬件检测到触摸操作,会将信息交给UIApplication,开始检测

UIApplication -> window -> viewController -> view -> 检测所有⼦视图

最终确认触碰位置,完成响应者链的查询过程。

处理触碰事件

检测到响应者后,实现touchesBegan:withEvent:等⽅法,即处理事件。

如果响应者没有处理事件,事件会向下传递。如果没有响应者处理,则丢弃触摸事件。

事件处理的顺序与触摸检测查询相反。

触摸的⼦视图 -> view -> viewController -> window -> UIApplication

阻断响应者链

响应者链可以被打断.无法完成检测查询过程

视图类的属性:userInteractionEnabled.关闭后能阻断查询过程

是视图的属性,控制器不能使用

你可能感兴趣的:(学习笔记)