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

{

   //处理长按事件

}



  















 




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

相关文章推荐

返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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