iOS传感器开发之——手势识别

一.手势种类

在iOS中常用的手势分为两种,一种是Touch事件,一种是继承于UIGestureRecognizer的手势识别类来识别。

在iOS中只有继承自UIResponder类的对象才能处理手势事件。

触摸事件

Touch事件一般由以下几个方法来处理事件

//触摸开始
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
//触摸移动
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
//触摸结束
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
//触摸取消,一般为非人为的触摸停止,如接入电话
- (void)touchesCancelled:(nullable NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;

使用Touch事件直接在相关视图类中重写以上方法即可

需要注意的事:

1.该类必须直接或者间接继承UIRespender

2.必须重写touchesBegan:withEvent:方法

3.视图的userInteractionEnabled属性必需是YES

4.视图的hidden属性必须不为YES

5.视图的透明值必须大于0.01

在以上方法中我们通过参数touches可以获得所有的UITouch值,UITouch用于存放你触摸事件。

//通过该方法从UITouch对象中获得位置信息。
//参数view是相对于哪个视图的位置,一般使用self
- (CGPoint)locationInView:(nullable UIView *)view;

 

下面是一个手指画线的Demo

  1 #import "MyDrawView.h"
  2 @interface MyDrawView (){
  3     CGPoint        _beginDrawPoint;
  4     CGPoint        _moveDrawoint;
  5 }
  6 @property (nonatomic, retain) NSMutableArray *lineAllArray;//储存所有线
  7 @property (nonatomic, retain) NSMutableArray *pointAllArray;//储存所有点
  8 @end
  9 
 10 @implementation MyDrawView
 11 
 12 
 13 #pragma mark - 初始化数据
 14 #pragma mark 初始化点的数组
 15 - (void)initMyPointArray {
 16     self.pointAllArray = nil;
 17     self.pointAllArray = [[NSMutableArray alloc] initWithCapacity:0];
 18 }
 19 
 20 #pragma mark 懒加载初始化线数组
 21 - (NSMutableArray *)lineAllArray {
 22     
 23     if (_lineAllArray == nil) {
 24         _lineAllArray = [[NSMutableArray alloc] initWithCapacity:0];
 25     }
 26     return _lineAllArray;
 27 }
 28 
 29 #pragma mark 插入点到点数组
 30 - (void)insertToPointArrayWithPoint:(CGPoint)sender {
 31     
 32     NSValue *pointValue = [NSValue valueWithCGPoint:sender];
 33     [self.pointAllArray addObject:pointValue];
 34 }
 35 
 36 #pragma mark 插入所有点到线数组
 37 - (void)insertToLineArrayWithPointArray{
 38     
 39     [self.lineAllArray addObject:self.pointAllArray];
 40 }
 41 
 42 
 43 #pragma mark - Touch事件
 44 //手指开始触屏开始
 45 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 46 {
 47     //从touches集合中拿到被触摸的点
 48     UITouch* touch=[touches anyObject];
 49     _beginDrawPoint = [touch locationInView:self];
 50     //初始化数组并插入点
 51     [self initMyPointArray];
 52     [self insertToPointArrayWithPoint:_beginDrawPoint];
 53 }
 54 //手指移动时候发出
 55 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
 56 {
 57     //从touches集合中拿到被触摸的点
 58     NSArray *movePointArray = [touches allObjects];
 59     _moveDrawoint =[[movePointArray objectAtIndex:0] locationInView:self];
 60     [self insertToPointArrayWithPoint:_moveDrawoint];
 61     [self setNeedsDisplay];
 62 }
 63 
 64 //当手指离开屏幕时候
 65 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
 66 {
 67     //在完成绘画时讲所有点存入线数组
 68     [self insertToLineArrayWithPointArray];
 69     [self setNeedsDisplay];
 70 }
 71 
 72 
 73 #pragma mark - drawRect方法
 74 - (void)drawRect:(CGRect)rect {
 75     
 76     //获取绘画上下文
 77     CGContextRef context=UIGraphicsGetCurrentContext();
 78     //设置笔冒
 79     CGContextSetLineCap(context, kCGLineCapRound);
 80     //设置画线的连接处 拐点圆滑
 81     CGContextSetLineJoin(context, kCGLineJoinRound);
 82     
 83     
 84     //绘画之前的线
 85     if (self.lineAllArray.count != 0) {
 86         [self drawRectBeforeLineWithContext:context];
 87     }
 88     //绘画之前的线
 89     if (self.pointAllArray.count >1) {
 90         [self drawRectCurrentLineWithContext:context];
 91     }
 92     
 93 }
 94 
 95 #pragma mark 绘画之前的线
 96 - (void)drawRectBeforeLineWithContext:(CGContextRef)context{
 97     for (NSInteger i = 0 ; i < self.lineAllArray.count; i++) {
 98         NSArray *pointArray = [NSArray arrayWithArray:self.lineAllArray[i]];
 99         [self drawRectLineWithContext:context withPointArray:pointArray];
100     }
101 }
102 
103 #pragma mark 绘画当前的线
104 - (void)drawRectCurrentLineWithContext:(CGContextRef)context {
105     [self drawRectLineWithContext:context withPointArray:[NSArray arrayWithArray:self.pointAllArray]];
106     
107 }
108 
109 #pragma mark 绘画线
110 - (void)drawRectLineWithContext:(CGContextRef)context withPointArray:(NSArray *)array {
111     //绘画开始路径
112     CGContextBeginPath(context);
113     //获得开始点
114     CGPoint startPoint = [array[0] CGPointValue];
115     //开始绘画点
116     CGContextMoveToPoint(context, startPoint.x, startPoint.y);
117     //绘画结束点
118     for (NSInteger j = 1; j < array.count; j ++) {
119         //获得结束点
120         CGPoint endPoint = [array[j] CGPointValue];
121         CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
122     }
123     
124     //绘制画笔的宽度
125 //    CGContextSetLineWidth(context, font);
126     //绘制画笔的颜色
127 //    CGContextSetStrokeColorWithColor(context,[color CGColor]);
128 //    CGContextSetFillColorWithColor (context, [color CGColor]);
129 
130     //闭合路径
131     CGContextStrokePath(context);
132 }
133 @end

 

手势识别

在iOS常用的有以下六类手势

手势 作用
UITapGestureRecognizer 点击手势
UIPinchGestureRecognizer 捏合手势
UIPanGestureRecognizer 拖拽手势
UISwipeGestureRecognizer 轻扫手势
UIRotationGestureRecognizer 旋转手势
UILongPressGestureRecognizer 长按手势

 

posted on 2015-12-28 16:02  ASK_L  阅读(117)  评论(0)    收藏  举报

导航