各种手势使用

作者: DVWang | 来源:发表于2017-09-08 15:29 被阅读0次

    Touch触摸
    // 碰到屏幕就会触发该方法

    • (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
      {
      NSLog(@"触碰到了");
      UITouch *touch = [touches anyObject];
      // 获取点击坐标
      CGPoint point = [touch locationInView:self.view];
      NSLog(@"%f, %f", point.x, point.y);
      }
      Tap点按
      UITapGestureRecognizer *doubleTgr = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTagr:)];
      // 点击的次数
      //doubleTgr.numberOfTapsRequired = 2;
      [self.imageView addGestureRecognizer:doubleTgr];
      LongPress长按
      UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lpgrStart:)];
      [self.imageView addGestureRecognizer:lpgr];
      Swipe轻扫
      // 由于轻扫只能识别一个方向,所以把他的四个方向放在一个数组里留着以后辨别,这样就能识别四个方向了
      NSArray *arr = @[[NSNumber numberWithUnsignedInteger:UISwipeGestureRecognizerDirectionDown],[NSNumber numberWithUnsignedInteger:UISwipeGestureRecognizerDirectionUp],[NSNumber numberWithUnsignedInteger:UISwipeGestureRecognizerDirectionLeft],[NSNumber numberWithUnsignedInteger:UISwipeGestureRecognizerDirectionRight]];
      for (int i=0; i<arr.count; i++) {
      UISwipeGestureRecognizer *swiper = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swiper:)];
      //把方向取出来
      NSNumber *direction = arr[i];
      // 把方向转为Integer类型赋给轻扫的方向
      swiper.direction = [direction unsignedIntegerValue];
      [self.imageView addGestureRecognizer:swiper];
      }
    • (void)swiper:(UISwipeGestureRecognizer *)swiper
      {
      if (swiper.direction == UISwipeGestureRecognizerDirectionDown) {
      NSLog(@"向下");
      }
      if (swiper.direction & UISwipeGestureRecognizerDirectionLeft) {
      NSLog(@"向左");
      }
      if (swiper.direction & UISwipeGestureRecognizerDirectionRight) {
      NSLog(@"向右");
      }
      if (swiper.direction & UISwipeGestureRecognizerDirectionUp) {
      NSLog(@"向上");
      }
      }
      Rotation旋转
      // 创建一个手势
      UIRotationGestureRecognizer *rotion = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotion:)];
      // 手势添加在控件上
      [self.imageView addGestureRecognizer:rotion];
    • (void)rotion:(UIRotationGestureRecognizer *)rotion
      {
      NSLog(@"%f", rotion.rotation);
      rotion.view.transform = CGAffineTransformRotate(rotion.view.transform, rotion.rotation);
      // 由于此方法是旋转过程中反复调用的,所以旋转弧度很容易叠加,要及时清零才正常一点
      rotion.rotation = 0;
      }
      Pinch捏合
      UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
      [self.imageView addGestureRecognizer:pinch];
    • (void)pinch:(UIPinchGestureRecognizer *)pinch
      {
      // 放大缩小的比例
      NSLog(@"%f", pinch.scale);

      pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
      // 把伸缩比例及时变为1,防止伸缩效果叠加
      pinch.scale = 1;
      }
      Pan拖拽
      UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
      [self.imageView addGestureRecognizer:pan] ;

    • (void)pan:(UIPanGestureRecognizer *)pan
      {
      // 记录原来的位置
      static CGPoint center;

      if (pan.state == UIGestureRecognizerStateBegan) {
      center = pan.view.center;
      }

      // 获取偏移量
      CGPoint point = [pan translationInView:self.view];
      // 最后拖拽手势所在view的中心点,就是原来的位置加上变动的位置
      pan.view.center = CGPointMake(center.x + point.x, center.y + point.y);
      }
      Double手势叠加
      //可以添加多个手势
      // 一个控件可以添加多个手势,但一个手势不能添加在多个控件上
      添加多个手势时要遵守<UIGestureRecognizerDelegate>协议

    // 返回yes,允许同时添加多个手势

    • (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
      {
      return YES;
      }

    相关文章

      网友评论

        本文标题:各种手势使用

        本文链接:https://www.haomeiwen.com/subject/tjzgjxtx.html