IOS开发基础(手势事件(UiGestureRecognizer))

【IOS开发基础(手势事件(UiGestureRecognizer))】在介绍之前,咱们首先要知道为什么要用手势。比如我们需要添加一些触屏、滑屏事件,比如捏合图片、双击屏幕、下拉视图等等,就会用到手势
一、手势(UIGestureRecognizer)的种类 其实UIGestureRecognizer这一类并不复杂,简单来说这就是一个特殊的触摸事件,UIGestureRecognizer是一个父类,而实际操作中我们要使用它的子类,、大概有以下几种:

子类名称 名称
UITapGestureRecognizer 轻拍手势
UISwipeGestureRecognizer 轻扫手势
UILongPressGestureRecognizer 长按手势
UIPanGestureRecognizer 平移手势
UIPinchGestureRecognizer 捏合(缩放)手势
UIRotationGestureRecognizer 旋转手势
UIScreenEdgePanGestureRecognizer 屏幕边缘平移
UISharkGestureRecognizer 晃动
二、创建手势 手势的创建的方法大同小异
  • UITapGestureRecognizer(轻拍手势)
//创建手势 使用initWithTarget:action:的方法创建 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)]; //设置属性 //tap 手势一共两个属性,一个是设置轻拍次数,一个是设置点击手指个数 //设置轻拍次数 tap.numberOfTapsRequired = 2; //设置手指字数 tap.numberOfTouchesRequired = 2; //别忘了添加到testView上 [self.view addGestureRecognizer:tap]; -(void)tapView:(UITapGestureRecognizer *)sender{ //设置轻拍事件改变testView的颜色 _testView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1]; }

  • UISwipeGestureRecognizer (轻扫手势)
//创建手势 UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeView:)]; //设置属性,swipe也是有两种属性设置手指个数及轻扫方向 swipe.numberOfTouchesRequired = 2; //设置轻扫方向(默认是从左往右) //direction是一个枚举值有四个选项,我们可以设置从左往右,从右往左,从下往上以及从上往下 //设置轻扫方向(默认是从左往右) swipe.direction = UISwipeGestureRecognizerDirectionLeft; [self.view addGestureRecognizer:swipe]; - (void)swipeView:(UISwipeGestureRecognizer *)sender { self.view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1]; //可在这里获取手势位置 }

  • UILongPressGestureRecognizer(长按手势)
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)]; //属性设置 //最小长按时间 longPress.minimumPressDuration = 2; [self.view addGestureRecognizer:longPress]; -(void)longPress:(UILongPressGestureRecognizer *)sender{ //进行判断,在什么时候触发事件 if (sender.state == UIGestureRecognizerStateBegan) { NSLog(@"长按状态"); //改变testView颜色 self.view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1]; } }

  • UIPanGestureRecognizer(拖拽手势)
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; [self.imageV addGestureRecognizer:pan]; 当手指拖动时调用 - (void)pan:(UIPanGestureRecognizer *)pan{拖动手势也有状态 if(pan.state == UIGestureRecognizerStateBegan){ 开始拖动 }else if(pan.state == UIGestureRecognizerStateChanged){ 注意:获取偏移量是相对于最原始的点 CGPoint transP = [pan translationInView:self.imageV]; self.imageV.transform = CGAffineTransformTranslate(self.imageV.transform, transP.x, transP.y); 复位,让它相对于上一次. [pan setTranslation:CGPointZero inView:self.imageV]; }else if(pan.state == UIGestureRecognizerStateEnded){ 结束拖动 } }

  • UIPinchGestureRecognizer(捏合手势,用于缩放)
  • UIRotationGestureRecognizer(旋转手势)
//现在视图上添加图片 对图片进行操作 //添加捏合手势 UIPinchGestureRecognizer *pinGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinGes:)]; [self.imageView addGestureRecognizer:pinGes]; 旋转时调用 - (void)pinGes:(UIPinchGestureRecognizer *)pin{ self.imageView.transform = CGAffineTransformScale(self.imageV.transform, pin.scale, pin.scale); 复位 [pin setScale:1]; }添加旋转手势 UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)]; 设置代理使其能够同时支持多个手势(捏合的时候同时支持旋转) rotation.delegate = self; [self.imageView addGestureRecognizer:rotation]; 当手指开始旋转时调用. - (void)rotation:(UIRotationGestureRecognizer *)rotation{ self.imageView.transform = CGAffineTransformRotate(self.imageV.transform, rotation.rotation); 复位. [rotation setRotation:0]; }

三、手势的一些代理函数(UIGestureRecognizerDelegate)
// 是否允许开始触发手势 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer; //手指触摸屏幕后回调的方法,返回NO则不再进行手势识别,方法触发等 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch; //开始进行手势识别时调用的方法,返回NO则结束,不再触发手势 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer; //是否支持多时候触发,返回YES,则可以多个手势一起触发方法,返回NO则为互斥 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer; //下面这个两个方法也是用来控制手势的互斥执行的 //这个方法返回YES,第一个手势和第二个互斥时,第一个会失效 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0); //这个方法返回YES,第一个和第二个互斥时,第二个会失效 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);

四、手势冲突处理 一般在含有UIScrollView或者UIScrollView的子类的视图上添加自己的手势就会有冲突;利用UIGestureRecognizerDelegate中的代理方法可以解决相应的冲突;
因为在UIScrollView上有手势,利用
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer; //设置为YES就可以一直传递下去

同时手势与事件触摸有优先级的关系,使用手势时应该多注意

    推荐阅读