触摸事件

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的触摸事件处理
UIView UIResponder 的子类,可以覆盖下列 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对象

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

事件的产生和传递
发生触摸事件后,系统会将该事件加入到一个由 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值