美文网首页
手势 Gesture

手势 Gesture

作者: 三思的简书 | 来源:发表于2019-05-17 21:48 被阅读0次
    • (void)createLabel {
      label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
      label.backgroundColor = [UIColor magentaColor];
      label.text = @"How old are you?";
      label.numberOfLines = 0;
      label.textColor = [UIColor blackColor];
      label.textAlignment = NSTextAlignmentCenter;
      label.userInteractionEnabled = YES;

      [self.view addSubview:label];
      }

    • (void)createRotation {
      UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGesture:)];
      UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGesture:)];
      UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];
      UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGesture:)];
      swipeGesture.numberOfTouchesRequired = 1;
      swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;

      [label addGestureRecognizer:swipeGesture];
      [label addGestureRecognizer:pan];
      [label addGestureRecognizer:pinch];
      [label addGestureRecognizer:rotation];
      }

    • (void)rotationGesture:(UIRotationGestureRecognizer *)rotation {
      label.transform = CGAffineTransformRotate(label.transform, rotation.rotation);
      // 复位
      rotation.rotation = 0;
      }

    -(void)pinchGesture:(UIPinchGestureRecognizer *)pinch {
    label.transform = CGAffineTransformScale(label.transform, pinch.scale, pinch.scale);
    // 复位
    pinch.scale = 1;
    }

    • (void)panGesture:(UIPanGestureRecognizer *)pan{

      //获取偏移量
      // 返回的是相对于最原始的手指的偏移量
      CGPoint transP = [pan translationInView:label];
      // 移动图片控件
      label.transform = CGAffineTransformTranslate(label.transform, transP.x, transP.y);
      // 复位,表示相对上一次
      [pan setTranslation:CGPointZero inView:label];
      }

    • (void)swipeGesture:(UISwipeGestureRecognizer *)swipe{
      NSLog(@"执行的swipe");
      swipe.direction = UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;
      }

    相关文章

      网友评论

          本文标题:手势 Gesture

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