【iOS开发-手势】iOS中各种手势总结

iOS中支持的手势事件

  1. UITapGestureRecognizer(敲击)
  2. UIPinchGestureRecognizer(捏合,用于缩放)
  3. UIPanGestureRecognizer(拖拽)
  4. UISwipeGestureRecognizer(轻扫)
  5. UIRotationGestureRecognizer(旋转)
  6. UILongPressGestureRecognizer(长按)

UITapGestureRecognizer(敲击)

//创建UITapGestureRecognizer对象,并且初始化的时候就绑定事件
  UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView)];

 //连击几次才触发事件   
recognizer.numberOfTapsRequired = 2;
 //在对应的View上添加这个手势监听   
[self.myView addGestureRecognizer:recognizer];

UILongPressGestureRecognizer(长按)

//创建UILongPressGestureRecognizer对象,并初始化的时候就绑定事件
    UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tapView)];

recognizer.minimumPressDuration = 2;//2秒后触发事件

[self.myView addGestureRecognizer:recognizer];

UISwipeGestureRecognizer(轻扫)

 //创建UISwipeGestureRecognizer对象
    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tapView)];
    //手势扫的方向
recognizer.direction = UISwipeGestureRecognizerDirectionLeft;

[self.myView addGestureRecognizer:recognizer];

UIRotationGestureRecognizer(旋转)

例子:旋转一个view

- (void)Rotate
{
//创建对象
    UIRotationGestureRecognizer *recognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];
    recognizer.delegate = self;//每一个View要想支持多个手势,就要实现这个代理
    [self.iconView addGestureRecognizer:recognizer];
}

//执行这个方法时候,会默认把旋转对象传过去
- (void)rotateView:(UIRotationGestureRecognizer *)recognizer
{
//recognizer.rotation等于旋转角度,加上前一个他的前一个角度,所以每次把前一个角度清0,这个值就等于旋转角度
//每次要把当前的transform+旋转的角度
    recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
    //要把旋转的角度设为0
    recognizer.rotation = 0; 
}

UIPinchGestureRecognizer(捏合)

- (void)Pinch
{
    //创建对象
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
    pinch.delegate = self;
    [self.iconView addGestureRecognizer:pinch];
}

- (void)pinchView:(UIPinchGestureRecognizer *)pinch
{
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    pinch.scale = 1; 
}

UIPanGestureRecognizer(拖拽)

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
    [self.purpleView addGestureRecognizer:pan];
}

- (void)panView:(UIPanGestureRecognizer *)pan
{

    switch (pan.state) {
        case UIGestureRecognizerStateBegan: // 开始触发手势

            break;

        case UIGestureRecognizerStateEnded: // 手势结束

            break;

        default:
            break;
    }

    // 1.在view上面挪动的距离
    CGPoint translation = [pan translationInView:pan.view];
    CGPoint center = pan.view.center;
    center.x += translation.x;
    center.y += translation.y;
    pan.view.center = center;

    // 2.清空移动的距离
    [pan setTranslation:CGPointZero inView:pan.view];
}

多手势的支持

目标View要想支持多个手势的必须实现UIGestureRecognizerDelegate代理
然后实现下面的方法


/**
 *  是否允许多个手势识别器同时有效
 *  Simultaneously : 同时地
 */
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。