触摸事件

来源:互联网 发布:南京市软件行业协会 编辑:程序博客网 时间:2024/04/30 16:49

iOS中的事件可以分为3大类型

     触摸事件                        加速计事件                    远程控制事件


iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件。我们称之为“响应者对象

UIResponder
UIResponder内部提供了以下方法来处理事件
触摸事件

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

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

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

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

加速计事件

- (void)motionBegan:(UIEventSubtype)motionwithEvent:(UIEvent *)event;

- (void)motionEnded:(UIEventSubtype)motionwithEvent:(UIEvent *)event;

- (void)motionCancelled:(UIEventSubtype)motionwithEvent:(UIEvent *)event;

远程控制事件
-(void)remoteControlReceivedWithEvent:(UIEvent *)event;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
触摸事件:
UIView的触摸事件处理
UIViewUIResponder的子类,可以覆盖下列4个方法处理不同的触摸事件
一根或者多根手指开始触摸view,系统会自动调用view的下面方法

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


一根或者多根手指在view上移动,系统会自动调用view的下面方法(随着手指的移动,会持续调用该方法)

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


一根或者多根手指离开view,系统会自动调用view的下面方法

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


触摸结束前,某个系统事件(例如电话呼入)会打断触摸过程,系统会自动调用view的下面方法

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

提示:touches中存放的都是UITouch对象


UITouch
当用户用手指触摸屏幕时,会创建一个与手指相关联的UITouch对象

一根手指对应一个UITouch对象

UITouch的作用
保存着跟手指相关的信息,比如触摸位置、时间、阶段
当手指移动时,系统会更新同一个UITouch对象,使之能够一直保存该手指在的触摸位置

当手指离开屏幕时,系统会销毁相应UITouch

提示:iPhone开发中,要避免使用双击事件
UITouch的属性
触摸产生时所处的窗

@property(nonatomic,readonly,retain)UIWindow    *window;


触摸产生时所处的视图

@property(nonatomic,readonly,retain)UIView      *view;


短时间内点按屏幕的次数,可以根据tapCount判断单击、双击或更多的点击

@property(nonatomic,readonly)NSUInteger          tapCount;


记录了触摸事件产生或变化时的时间,单位是

@property(nonatomic,readonly)NSTimeInterval      timestamp;


当前触摸事件所处的状态

@property(nonatomic,readonly)UITouchPhase        phase;

UITouch的方法
- (CGPoint)locationInView:(UIView*)view;
返回值表示触摸在view(控件)上的位置
这里返回的位置是针对view的坐标系的(以view的左上角为原点(0,0)
调用时传入view参数为nil的话,返回的是触摸点在UIWindow的位置

- (CGPoint)previousLocationInView:(UIView*)view;
该方法记录了前一个触摸点的位置

UIEvent
每产生一个事件,就会产生一个UIEvent对象

UIEvent:称为事件对象,记录事件产生的时刻类型

常见属性
事件类型

@property(nonatomic,readonly)UIEventType     type;

@property(nonatomic,readonly)UIEventSubtype  subtype;


事件产生的时间

@property(nonatomic,readonly)NSTimeInterval  timestamp;

UIEvent还提供了相应的方法可以获得在某个view上面的触摸对象(UITouch

touches和event参数
一次完整的触摸过程,会经历3个状态:
触摸开始:- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event
触摸移动:- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
触摸结束:- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
触摸取消(可能会经历):- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event
4个触摸事件处理方法中,都有NSSet*touches和UIEvent*event两个参数
一次完整的触摸过程中,只会产生一个事件对象,4个触摸方法都是同一个event参数

如果两根手指同时触摸一个view,那么view只会调用一次touchesBegan:withEvent:方法,touches参数中装着2个UITouch对象

如果这两根手指一前一后分开触摸同一个view,那么view会分别调用2次touchesBegan:withEvent:方法,并且每次调用时的touches参数中只包含一个UITouch对象

根据touchesUITouch的个数可以判断出是单点触摸还是多点触摸

事件的产生和传递
发生触摸事件后,系统会将该事件加入到一个由UIApplication管理的事件队列中

UIApplication会从事件队列中取出最前面的事件,并将事件分发下去以便处理,通常,先发送事件给应用程序的主窗口(keyWindow

主窗口会在视图层次结构中找到一个最合适的视图来处理触摸事件,这也是整个事件处理过程的第一步

找到合适的视图控件后,就会调用视图控件的touches方法来作具体的事件处理

触摸事件的传递是从父控件传递到子控件

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
涂鸦案例:

效果图:

代码:
Main.storyboard

ViewController.m
#import "ViewController.h"#import "CZPointView.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet CZPointView *paintView;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.}// 回退按钮的单击事件- (IBAction)backBtnClick {        [self.paintView back];    }// 清屏按钮的单击事件- (IBAction)clearScreen:(id)sender {        [self.paintView clear];}// 保存按钮的单击事件- (IBAction)saveBtnClick:(id)sender {        [self.paintView save];}// 设置线条颜色的按钮的单击事件- (IBAction)setColor:(UIButton *)sender {    self.paintView.currentColor = sender.backgroundColor;}

CZPointView.h
#import <UIKit/UIKit.h>@interface CZPointView : UIView// 保存当前颜色@property (nonatomic, strong) UIColor *currentColor;// 返回一步- (void)back;// 清屏- (void)clear;// 保存- (void)save;@end

CZPointView.m
@interface CZPointView ()// 保存 “所有线”的点@property (nonatomic, strong) NSMutableArray *pointOfAllLine;// 保存线的颜色@property (nonatomic, strong) NSMutableArray *colorOfLine;@end@implementation CZPointView// 懒加载- (NSMutableArray *)pointOfAllLine{    if (!_pointOfAllLine) {        _pointOfAllLine = [NSMutableArray array];    }    return _pointOfAllLine;}- (NSMutableArray *)colorOfLine{    if (!_colorOfLine) {        _colorOfLine = [NSMutableArray array];    }    return _colorOfLine;}- (void)drawRect:(CGRect)rect{        // 创建一个图层上下文    CGContextRef layerContext = UIGraphicsGetCurrentContext();        // 设置线宽    CGContextSetLineWidth(layerContext, 2);    // 设置线首尾样式与连接点的样式    CGContextSetLineCap(layerContext, kCGLineCapRound);    CGContextSetLineJoin(layerContext, kCGLineJoinRound);        // 遍历每条线    NSInteger lineCount = self.pointOfAllLine.count;    for (NSInteger i = 0; i < lineCount; i++) {        // 取出当前第一条线的数组        NSArray *pointsOfALine = self.pointOfAllLine[i];                // 设置线的颜色        UIColor *lineColor = self.colorOfLine[i];        [lineColor set];                // 取出线的点进行绘制        NSInteger pointsCount = pointsOfALine.count;        for (NSInteger j = 0; j < pointsCount; j++) {            CGPoint point = [pointsOfALine[j] CGPointValue];            if (j == 0) {                CGContextMoveToPoint(layerContext, point.x, point.y);            }else{                CGContextAddLineToPoint(layerContext, point.x, point.y);            }        }        // 渲染        CGContextStrokePath(layerContext);    }   }- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    // 创建一个数组用来存放一条线的点    NSMutableArray *pointOfALine = [NSMutableArray array];        // 将这条线添加到所有线的数组中    [self.pointOfAllLine addObject:pointOfALine];        // 保存当前 “一条线”的颜色    if (!self.currentColor) { // 如果为空就给个默认颜色:黑色        [self.colorOfLine addObject:[UIColor blackColor]];    }else{        [self.colorOfLine addObject:self.currentColor];    }    }- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{        // 获取当前的UITouch对象    UITouch *touch = [touches anyObject];        // 获取当前手势的位置    CGPoint touchPoint = [touch locationInView:touch.view];        // 将获取的位置添加到 “一条线” 数组中    // 获取当前线的数组    NSMutableArray *pointsOfLine = [self.pointOfAllLine lastObject];    [pointsOfLine addObject:[NSValue valueWithCGPoint:touchPoint]];        // 根据触摸的点进行绘制    [self setNeedsDisplay];}// 清空- (void)clear{    // 删除所有的线与颜色    [self.pointOfAllLine removeAllObjects];    [self.colorOfLine removeAllObjects];    // 重新绘制    [self setNeedsDisplay];   }// 返回上一步- (void)back{    // 删除最后一条线    [self.pointOfAllLine removeLastObject];        // 重新绘制    [self setNeedsDisplay];}// 保存- (void)save{        // 开启位图上下文    UIGraphicsBeginImageContext(self.bounds.size);        // 把当前view画在位图上下文    [self.layer renderInContext:UIGraphicsGetCurrentContext()];        // 获取图片    UIImage *captureImage = UIGraphicsGetImageFromCurrentImageContext();        // 结束编辑    UIGraphicsEndImageContext();        // 保存图片到沙盒中    // 获取document目录    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];    // 拼接完整路径    NSString *path = [document stringByAppendingPathComponent:@"capture.png"];        // 将图片保存为NSData类型    NSData *imageData = UIImagePNGRepresentation(captureImage);    // 写入路径    [imageData writeToFile:path atomically:YES];    }@end















0 0
原创粉丝点击