iOS 各种手势集合

原创 2015年07月06日 16:20:32

 ios目前支持6中手势识别。

  1. UITapGestureRecognizer  : 点击手势
  2. UIPinchGestureRecognizer  : 捏合手势
  3. UIPanGestureRecognizer  :  拖动手势
  4. UISwipeGestureRecognizer  : 滑动手势
  5. UIRotationGestureRecognizer  : 旋转手势
  6. UILongPressGestureRecognizer  : 长按手势
具体代码执行:
  •  点击手势   

UITapGestureRecognizer* recognizer;

    recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom)];

    recognizer.numberOfTapsRequired = 1; // 点击次数

    recognizer.numberOfTouchesRequired = 1;//手指个数

    [self.view addGestureRecognizer:recognizer];

- (void)handleTapFrom:(UITapGestureRecognizer *)recognizer

{

recognizer.view.tag//添加手势的视图的tag值

//处理点击事件

}

    • 捏合手势

 //自己设置 imageviewimage

    UIImageView *imageview;

    UIImage *srcImage ;

 //设置imageview允许用户交互,支持多点触碰

    imageview.userInteractionEnabled = YES;

    imageview.multipleTouchEnabled = YES;

    UIPinchGestureRecognizer* recognizer;

    recognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlepinFrom:)];

    [imageview addGestureRecognizer:recognizer];

- (void)handlepinFrom:(UIPinchGestureRecognizer *)gesture

{

    CGFloat currentScale;

    CGFloat scale = gesture.scale;

    // 如果捏合手势刚刚开始

    if (gesture.state == UIGestureRecognizerStateBegan)

    {

        // 计算当前缩放比

        currentScale = imageview.image.size.width / srcImage.size.width;

    }

    // 根据手势处理器的缩放比例计算图片缩放后的目标大小

    CGSize targetSize = CGSizeMake(srcImage.size.width * scale * currentScale,

    srcImage.size.height * scale * currentScale);

    // 对图片进行缩放

    imageview.image = [srcImage imageByScalingToSize:targetSize];

}

  •  拖动手势
           

//自己设置 imageview

   UIImageView *imageview;

 //设置imageview允许用户交互,支持多点触碰

    imageview.userInteractionEnabled = YES;

    imageview.multipleTouchEnabled = YES;

    UIPanGestureRecognizer* recognizer;

    recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlepinFrom:)];

    //最小手指数

    recognizer.minimumNumberOfTouches = 1;

    //最大手指数

    recognizer.maximumNumberOfTouches = 1;

    [imageview addGestureRecognizer:recognizer];

- (void)handlepinFrom:(UIPanGestureRecognizer *)gesture

{

 //处理坐标

}

  •  滑动手势
  

//单独手势一个一个的添加

    UISwipeGestureRecognizer *recognizer;


       recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];

     [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];

    [self.view addGestureRecognizer:recognizer];


    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];

    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];

    [self.view addGestureRecognizer:recognizer];


    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];

    [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];

    [self.view addGestureRecognizer:recognizer];

    

      recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];

    [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];

    [[self.view addGestureRecognizer:recognizer];

   

 -(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{

                 if(recognizer.direction==UISwipeGestureRecognizerDirectionDown) {

                         NSLog(@"swipe down");

             //执行程序

         }

         if(recognizer.direction==UISwipeGestureRecognizerDirectionUp) {

                         NSLog(@"swipe up");

             //执行程序

         }

         

         if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft) {

                         NSLog(@"swipe left");

             //执行程序

         }

         

         if(recognizer.direction==UISwipeGestureRecognizerDirectionRight) {

                         NSLog(@"swipe right");

             //执行程序

         }

        }

  • 旋转手势

 UIImageView *imageview;

 UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];

 [imageview addGestureRecognizer:rotationGestureRecognizer];


- (void) rotateView:(UIRotationGestureRecognizer *)rotationGestureRecognizer

{

    UIView *view = rotationGestureRecognizer.view;

    if (rotationGestureRecognizer.state == UIGestureRecognizerStateBegan || rotationGestureRecognizer.state == UIGestureRecognizerStateChanged) {

        view.transform = CGAffineTransformRotate(view.transform, rotationGestureRecognizer.rotation);

        [rotationGestureRecognizer setRotation:0];

    }

}

  • 长按手势

UIImageView *imageview;

    UILongPressGestureRecognizer * longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo:)];

    longPressGr.minimumPressDuration = 1.0;//最短时间

    [imageview addGestureRecognizer:longPressGr];

- (void) longPressToDo:(UILongPressGestureRecognizer *)gnizer

{

   //处理长按事件

}



  















 




版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

IOS各种手势代码

  • 2013年11月25日 16:20
  • 4KB
  • 下载

ios手势程序

  • 2014年08月20日 15:56
  • 1.05MB
  • 下载

iOS开发之自定义导航栏返回按钮右滑返回手势失效的解决

什么时候会被拦截 当我们使用代码或者用storyboard来创建一个新的控制器的时候~我们很少注意导航栏的leftItem~因为正常情况下~push到下一个界面的leftItem就是上一个界面的sel...
  • hxpp777
  • hxpp777
  • 2016年08月03日 11:44
  • 1403

IOS_手势识别器-旋转+缩放

  • 2015年06月02日 10:18
  • 424B
  • 下载

iOS开发(OC)——单击手势与tableView的点击冲突

不知道各位在做开发的时候,是否有在同一个页面里既有单击的手势,又有tableView的存在的情况,如果有的话,你可能会发现,你的tableView点击出问题了,打印一下,你会发现,你单击tableVi...

ios手势密码

  • 2015年09月18日 16:43
  • 3KB
  • 下载

IOS手势解锁

  • 2016年07月12日 11:56
  • 430KB
  • 下载

ios手势识别(双击、捏、旋转、拖动、划动、长按, 上下左右滑动)

ios的手势操作之UIGestureRecognizer浅析 一、概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:iOS 各种手势集合
举报原因:
原因补充:

(最多只允许输入30个字)