ios触摸事件案例

2018-05-24  本文已影响0人  浅_若清风

前言:ios事件之触摸事件详解请跳转https://www.jianshu.com/p/1c355a7264b5
创建画布画线条
1、创建模型对象并声明CGPoint属性

.h文件
@interface TouchLine : NSObject
/*起点*/
@property(nonatomic,assign)CGPoint  begin;
/*终点*/
@property(nonatomic,assign)CGPoint  end;

@end

.m文件
@implementation TouchLine

@synthesize begin,end;

@end

2、自定义视图,创建TouchDrawView类,继承于UIView

.h文件
@interface TouchDrawView : UIView{
    //存储key值
    NSMutableDictionary *lineInProcess_Dic;
    //存储key对应的值
    NSMutableArray *completeline_Arr;
}

/*清理*/
-(void)clearAll;

.m文件
@implementation TouchDrawView

-(id)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        //初始化
        lineInProcess_Dic=[[NSMutableDictionary alloc]init];
        completeline_Arr=[[NSMutableArray alloc]init];
        [self setBackgroundColor:[UIColor whiteColor]];
        //开启多点触摸事件,默认为NO
        [self setMultipleTouchEnabled:YES];
    }
    return self;
}

/*C函数绘制线条*/
-(void)drawRect:(CGRect)rect{
    //设置上下文
    CGContextRef context=UIGraphicsGetCurrentContext();
    //设置线宽
    CGContextSetLineWidth(context, 10.0);
    //设置线条终点形状
    CGContextSetLineCap(context, kCGLineCapRound);//kCGLineCapRound属性值指定绘制圆形端点, 线条结尾处绘制一个直径为线条宽度的半圆
    //设置当前绘图上下文中的填充和笔划颜色(已经完成的线条)
    [[UIColor blackColor]set];
    for (TouchLine *line in completeline_Arr) {
        //开始画线
        CGContextMoveToPoint(context, [line begin].x, [line begin].y);
        //画直线
        CGContextAddLineToPoint(context, [line end].x, [line end].y);
        //沿着路径画线
        CGContextStrokePath(context);
    }
    
    //设置当前绘图上下文中的填充和笔划颜色(正在画的线条)
    [[UIColor redColor]set];
    for (NSValue *value in lineInProcess_Dic) {
        TouchLine *line = [lineInProcess_Dic objectForKey:value];
        //开始画线
        CGContextMoveToPoint(context, [line begin].x, [line begin].y);
        //画直线
        CGContextAddLineToPoint(context, [line end].x, [line end].y);
        //沿着路径画线
        CGContextStrokePath(context);
    }
    
}

-(void)clearAll{
    [lineInProcess_Dic removeAllObjects];
    [completeline_Arr removeAllObjects];
    //重画
    [self setNeedsDisplay];
}


//一根手指或多根手指触摸屏幕
- (void)touchesBegan:(NSSet *)touches withEvent:(nullable UIEvent *)event{
    for (UITouch *touch in touches) {
        //连按
        if ([touch tapCount]>1) {
            [self clearAll];
            return;
        }
        //封装UITouch对象地址至value_key中
        NSValue *value_key = [NSValue valueWithNonretainedObject:touch];
        //根据触摸位置创建对象
        CGPoint locat = [touch locationInView:self];
        TouchLine *line = [[TouchLine alloc]init];
        [line setBegin:locat];
        [line setEnd:locat];
        //将键-值对保存至字典中
        [lineInProcess_Dic setObject:line forKey:value_key];
    }
}

//一根手指或多根手指在屏幕上移动(随着手指的移动,相关的对象会持续发送该消息)
- (void)touchesMoved:(NSSet *)touches withEvent:(nullable UIEvent *)event{
    //根据传入的UITouch对象,更新lineInProcess_Dic
    for (UITouch *touch in touches) {
        //取出当前对应UITouch对象的TouchLine对象
        NSValue *value_key = [NSValue valueWithNonretainedObject:touch];
        TouchLine *line = [lineInProcess_Dic objectForKey:value_key];
        //更新TouchLine对象
        CGPoint locat = [touch locationInView:self];
        [line setEnd:locat];
    }
    //重画
    [self setNeedsDisplay];
}

//提取的公共方法
-(void)endTouches:(NSSet *)touches{
    //将已经完成的TouchLine对象移除
    for (UITouch *touch in touches) {
        //取出当前对应UITouch对象的TouchLine对象
        NSValue *value_key = [NSValue valueWithNonretainedObject:touch];
        TouchLine *line = [lineInProcess_Dic objectForKey:value_key];
        //如果是连按,则line值为nil,所以先判断,避免nil加入数组
        if (line) {
            [completeline_Arr addObject:line];
            [lineInProcess_Dic removeObjectForKey:value_key];
        }
    }
    //重画
    [self setNeedsDisplay];
}

//一根手指或多根手指离开屏幕
- (void)touchesEnded:(NSSet *)touches withEvent:(nullable UIEvent *)event{
    [self endTouches:touches];
}

//在触摸操作正常结束前,某个系统事件(如有电话打进来)打断了触摸过程
- (void)touchesCancelled:(NSSet *)touches withEvent:(nullable UIEvent *)event{
    [self endTouches:touches];
}

//3D 触摸事件
- (void)touchesEstimatedPropertiesUpdated:(NSSet *)touches NS_AVAILABLE_IOS(9_1){
    NSLog(@"暂时没找到使用的方法案例");
}

3、在UIViewController的.m文件中调用

@implementation TouchVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title=@"触摸事件";
    TouchDrawView *touchView = [[TouchDrawView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT)];
    [self.view addSubview:touchView];
}
上一篇下一篇

猜你喜欢

热点阅读