IOS触摸事件和手势识别

IOS触摸事件和手势识别

 

目录

  • 概述
  • 触摸事件
  • 手势识别

 

概述

为了实现一些新的需求,我们常常需要给IOS添加触摸事件和手势识别

 

触摸事件

触摸事件的四种方法

-(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  系统由于某原因取消触摸事件时所调用的方法

获得触摸位置的方法

for(UITouch *t in touches){

CGPoint touchPosition = [t locationInView:self.view];

NSLog(@"%f,%f",touchPosition.x,touchPosition.y);  

}

  

手势识别

手势种类

UITapGestureRecognizer轻触

UISwipeGestureRecognizer很快的滑动

UIPanGestureRecognizer拖动

UIPinchGestureRecognizer两个手指头捏或放

UIRotationGestureRecognizer手指方向操作

UILongPressGestureRecognizer长按

UITapGestureRecognizer轻触

初始化

UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

[view addGestureRecognizer:tapGR];

常用方法

[self setNumberOfTapsRequired:numbers];

[self setNumberOfTouchesRequired:number2];

其他方法可以参考UIGestureRecognizer文档

UITapGestureRecognizer的响应方法

-(void)tapAction:(UITapGestureRecognizer *)sender{

//Tap拿起的时候

if(tapGR.state == UIGestureRecognizerStateEnded){

}

//Tap按下的时候

if(tapGR.state == UIGestureRecognizerStateBegan){

}

//等等其他状态

}

获得UITapGestureRecognizer按下的位置

CGPoint point = [sender locationInView:view];

 

 

posted @ 2014-11-24 14:14  WongBob  阅读(224)  评论(0编辑  收藏  举报