手势识别功能(Gesture Recognizer)


1. 手势识别概念

a. iOS 3.2之后,苹果推出了手势识别功能(GestureRecognizer),在触摸事件处理方面,大大简化了开发者的开发难度。

b. 为了完成手势识别,必须借助于手势识别器----UIGestureRecognizer,利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势。

2. 手势识别功能

UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势:

UITapGestureRecognizer(敲击)

UIPinchGestureRecognizer(捏合,用于缩放)

UIPanGestureRecognizer(拖拽)

UISwipeGestureRecognizer(轻扫)

UIRotationGestureRecognizer(旋转)

UILongPressGestureRecognizer(长按)

3. 手势识别用法

a. 每一个手势识别器的用法都这三步:

1> 创建一个手势的对象;

2> 把手势的对象添加到需要手势的view当中;

3> 实现手势的方法;

b. 用法的具体介绍

#import "ViewController.h"

@interfaceViewController ()

@property (weak,nonatomic) IBOutletUIImageView* imageView;

@end

@implementationViewController

- (void)viewDidLoad

{

[superviewDidLoad];

//敲击手势


// 创建敲击手势的对象

UITapGestureRecognizer* tap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tap:)];

// 几根手指

tap.numberOfTouchesRequired = 2;

// 点几次

tap.numberOfTapsRequired = 2;

// imageView添加手势

[self.imageViewaddGestureRecognizer:tap];

//轻扫手势


// 创建轻扫手势的对象

UISwipeGestureRecognizer* swipe = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipe:)];

UISwipeGestureRecognizer* swipe1 = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipe:)];

// 设置手势方向

swipe.direction = UISwipeGestureRecognizerDirectionLeft;

swipe.direction = UISwipeGestureRecognizerDirectionRight;

// imageView添加手势

[self.imageViewaddGestureRecognizer:swipe];

[self.imageViewaddGestureRecognizer:swipe1];

//长按手势


UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPress:)];

// 长按多长时间执行方法

longPress.minimumPressDuration = 2;

// 误差

longPress.allowableMovement = 10;

// imageView添加手势

[self.imageViewaddGestureRecognizer:longPress];

//旋转


UIRotationGestureRecognizer* rotation = [[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotation:)];

[self.imageViewaddGestureRecognizer:rotation];

//捏合,用于缩放


UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinch:)];

[self.imageViewaddGestureRecognizer:pinch];

//拖拽


UIPanGestureRecognizer* pan = [[UIPanGestureRecognizeralloc] initWithTarget:selfaction:@selector(pan:)];

[self.imageViewaddGestureRecognizer:pan];

}

// 敲击


- (void)tap:(UITapGestureRecognizer*)sender

{

NSLog(@"tap");

}

// 轻扫


- (void)swipe:(UISwipeGestureRecognizer*)sender

{

if (sender.direction == UISwipeGestureRecognizerDirectionLeft){

NSLog(@"left");

}

else {

NSLog(@"right");

}

}

// 长按


- (void)longPress:(UILongPressGestureRecognizer*)sender

{

// 想让长按开始的时候执行某个代码,需要判断手势的状态

if (sender.state == UIGestureRecognizerStateBegan){

NSLog(@"longPress");

}

}

UIGestureRecognizerState手势状态是一个枚举

typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {

// 没有触摸事件发生,所有手势识别的默认状态

UIGestureRecognizerStatePossible,

// 一个手势已经开始但尚未改变或者完成时

UIGestureRecognizerStateBegan,

// 手势状态改变

UIGestureRecognizerStateChanged,

// 手势完成

UIGestureRecognizerStateEnded,

// 手势取消,恢复至Possible状态

UIGestureRecognizerStateCancelled,

// 手势失败,恢复至Possible状态

UIGestureRecognizerStateFailed,

// 识别到手势识别

UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded

};

// 旋转


- (void)rotation:(UIRotationGestureRecognizer*)sender

{

self.imageView.transform= CGAffineTransformRotate(self.imageView.transform, sender.rotation);

sender.rotation = 0;

}

// 捏合


- (void)pinch:(UIPinchGestureRecognizer*)sender

{

self.imageView.transform= CGAffineTransformScale(self.imageView.transform, sender.scale,sender.scale);

sender.scale = 1;

}

// 拖拽


- (void)pan:(UIPanGestureRecognizer*)sender

{

//手指拖动imageView位移时的实时位移量;

CGPoint p = [sender translationInView:self.imageView];

NSLog(@"%@",NSStringFromCGPoint(p));

//imageView按照上面的实时位移量进行位移;

self.imageView.transform= CGAffineTransformTranslate(self.imageView.transform, p.x,p.y);

//每一次实时位移的时候,都让它的位移量归零;

[sender setTranslation:CGPointZeroinView:self.imageView];

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值