iOS开发各种手势识别

iOS开发中手势识别有六种: 

轻击手势(TapGestureRecognizer), 

轻扫手势 (SwipeGestureRecognizer), 

长按手势(LongPressGestureRecognizer), 

拖动手势(PanGestureRecognizer), 

捏合手势(PinchGestureRecognizer), 

旋转手势(RotationGestureRecognizer),

使用手势很简单,分为两步:

创建手势实例。当创建手势时,指定一个回调方法,当手势开始,改变、或结束时,回调方法被调用。

添加到需要识别的View中。每个手势只对应一个View,当屏幕触摸在View的边界内时,如果手势和预定的一样,那就会回调方法。

1,点击手势(TapGestureRecognizer)

  1. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    tapGesture.numberOfTapsRequired = 1; //点击次数
    tapGesture.numberOfTouchesRequired = 1; //点击手指数
    [self.view addGestureRecognizer:tapGesture];
     
    //轻击手势触发方法
    -(void)tapGesture:(UITapGestureRecognizer *)sender
    {
        //your code
    }

2,长按手势(LongPressGestureRecognizer)

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
//设置长按时间
longPressGesture.minimumPressDuration = 0.5;
[self.view addGestureRecognizer:longPressGesture];
//长按手势触发方法
-(void)longPressGesture:(id)sender
{
     UILongPressGestureRecognizer *longPress = sender;
     if (longPress.state == UIGestureRecognizerStateBegan)
     {
         //your code
     }
}
说明:长按手势的常用状态如下
开始:UIGestureRecognizerStateBegan
改变:UIGestureRecognizerStateChanged
结束:UIGestureRecognizerStateEnded
取消:UIGestureRecognizerStateCancelled
失败:UIGestureRecognizerStateFailed

3,旋转手势(RotationGestureRecognizer)

UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
[self.view addGestureRecognizer:rotationGesture];
//旋转手势触发方法
-(void)rotationGesture:(id)sender
{
    UIRotationGestureRecognizer *gesture = sender;
    if (gesture.state==UIGestureRecognizerStateChanged)
    {
        _imageView.transform=CGAffineTransformMakeRotation(gesture.rotation);
    }
    if(gesture.state==UIGestureRecognizerStateEnded)
    {
        [UIView animateWithDuration:1 animations:^{
            _imageView.transform=CGAffineTransformIdentity;//取消形变
        }];
    }
}

4,拖动手势(PanGestureRecognizer)

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[self.view addGestureRecognizer:panGesture];
 
//拖动手势触发方法
-(void) panGesture:(id)sender
{
    UIPanGestureRecognizer *panGesture = sender;
    CGPoint movePoint = [panGesture translationInView:self.view];
    //your code
}


5,轻扫手势(SwipeGestureRecognizer)

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//设置轻扫的方向
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //向右
[self.view addGestureRecognizer:swipeGesture];
UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//设置轻扫的方向
swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //向左
[self.view addGestureRecognizer:swipeGestureLeft];
//轻扫手势触发方法
-(void)swipeGesture:(id)sender
{
    UISwipeGestureRecognizer *swipe = sender;
    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        //向左轻扫
    }
    if (swipe.direction == UISwipeGestureRecognizerDirectionRight)
    {
        //向右轻扫
    }
}

6,捏合手势(PinchGestureRecognizer)

UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
[self.view addGestureRecognizer:pinchGesture];
    捏合手势触发方法
-(void) pinchGesture:(id)sender
{
    UIPinchGestureRecognizer *gesture = sender;
    //手势改变时
    if (gesture.state == UIGestureRecognizerStateChanged)
    {
        self.View.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);
    }
  
}






转载于:https://my.oschina.net/u/2532565/blog/552458

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值