1、首先来介绍下触摸事件和手势识别器的利与弊
触摸事件和手势识别器二者之间有直接的关系 手势识别器是在触摸事件的基础上演变过来的 当我们用到触摸事件时 默认的uiview是没有什么效果的 只能自定义view才能实现事件的触摸 通常用到的方法如下: – touchesBegan:withEvent: – touchesMoved:withEvent: – touchesEnded:withEvent: - touchesCancelled:withEvent: 而手势识别器是在触摸事件的基础上而封装的 为什么要封装呢?因为直接touchs方法来定位手势比较麻烦。在还没有UIGestureRecognizer的时代,用touchs也可以计算出博文中说的几种常用手势:点,滑动,拖等等。这些手势都非常常用,每个开发者想要监视这些手势的时候都要自己判断一遍,每个人都在重复造轮子。那把这些手势封装出来标准化,就有了UIGestureRecognizer和它对应的子类手势了。 那为什么还要有touchs等方法存在呢?因为UIGestureRecognizer几种手势比较有限,有的游戏应用需要搞些特别的手势,那就用touchs这些方法来定义了。
2、使用手势步骤
使用手势很简单,分为两步: 创建手势实例。当创建手势时,指定一个回调方法,当手势开始,改变、或结束时,回调方法被调用。 添加到需要识别的View中。每个手势只对应一个View,当屏幕触摸在View的边界内时,如果手势和预定的一样,那就会回调方法。 ps:一个手势只能对应一个View,但是一个View可以有多个手势。 建议在真机上运行这些手势,模拟器操作不太方便,可能导致你认为手势失效。
3、手势的介绍
UITapGestureRecognizer (敲击) UIPinchGestureRecognizer (捏合,由于缩放) UIPanGestureRecognizer (拖拽) UISwipeGestureRecognizer (轻扫) UIRotationGestureRecognizer(旋转) UILongPressGestureRecognizer(长按)
4、敲击手势的用法(其他手势类同)代码如下:
//创建手势识别对象 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init]; //连续敲击二次手势才能识别成功 tap.numberOfTapsRequired = 2; //需要一根手指触摸 tap.numberOfTouchesRequired = 2; //添加手势识别器对象到对应的uiview [self.iconView addGestureRecognizer:tap]; //添加监听方法(识别到对应的手势就会监听事件) [tap addTarget:self action:@selector(btClick:)];
5、缩放 + 旋转
#import "ViewController.h" @interface ViewController ()<UIGestureRecognizerDelegate> @property (weak, nonatomic) IBOutlet UIImageView *iconView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.iconView.userInteractionEnabled = YES; // Do any additional setup after loading the view, typically from a nib. [self testPichAndRoate]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void) testPichAndRoate { //[self testPich]; [self testRoate]; } -(void) testPich { UIPinchGestureRecognizer *pich = [[UIPinchGestureRecognizer alloc] init]; pich.delegate = self; [self.iconView addGestureRecognizer:pich]; [pich addTarget:self action:@selector(pinchView:)]; } -(void) pinchView:(UIPinchGestureRecognizer *)pinchView { pinchView.view.transform = CGAffineTransformScale(pinchView.view.transform, pinchView.scale, pinchView.scale); pinchView.scale = 1;//必写 } -(void) testRoate { UIRotationGestureRecognizer *roate = [[UIRotationGestureRecognizer alloc] init]; roate.delegate = self; [self.iconView addGestureRecognizer:roate]; [roate addTarget:self action:@selector(roateView:)]; } -(void) roateView:(UIRotationGestureRecognizer *)roate { roate.view.transform = CGAffineTransformRotate(roate.view.transform, roate.rotation); roate.rotation = 0;//必写 } @end
6、拖动
#import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIView *panView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] init]; [self.panView addGestureRecognizer:pan]; [pan addTarget:self action:@selector(panTuoDong:)]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)panTuoDong:(UIPanGestureRecognizer *)pan { switch (pan.state) { case UIGestureRecognizerStateBegan: // 开始触发手势 break; case UIGestureRecognizerStateEnded: // 手势结束 break; default: break; } //在view上挪动的距离 CGPoint transtation = [pan translationInView:pan.view]; CGPoint center = pan.view.center; center.x += transtation.x; center.y += transtation.y; pan.view.center = center; //清除挪动的距离 [pan setTranslation:CGPointZero inView:pan.view]; } @end