UISwipeGestureRecognizer各种手势的用法

 //单击

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:selfaction:@selector(handleSingleTap:)];

    [self.viewaddGestureRecognizer:singleTap];

    

    //双击

    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:selfaction:@selector(handleDoubleTap:)];

    //设置点击次数

    [doubleTap setNumberOfTapsRequired:2];

    [self.view addGestureRecognizer:doubleTap];

    

    //区别单击、双击

    [singleTap requireGestureRecognizerToFail:doubleTap];


   

    

    //创建一个手势识别器

    swipeGestureRecognizer=[[[UISwipeGestureRecognizer alloc]initWithTarget:selfaction:@selector(swipeGesture:)]autorelease];

    //设置几个手指

    [swipeGestureRecognizer setNumberOfTouchesRequired:1];

    //设置轻扫方向

//    [swipeGestureRecognizer setDirection:UISwipeGestureRecognizerDirectionUp];

    [swipeGestureRecognizer setDirection:UISwipeGestureRecognizerDirectionDown];

//    [swipeGestureRecognizer setDirection:UISwipeGestureRecognizerDirectionLeft];

//    [swipeGestureRecognizer setDirection:UISwipeGestureRecognizerDirectionRight];

    //设置代理

    [swipeGestureRecognizer setDelegate:self];

    //添加Gestureview

    [self.view addGestureRecognizer:swipeGestureRecognizer];

    

    

    //平移手势

    panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:selfaction:@selector(panGesture:)];

    [viewImg2 addGestureRecognizer:panGesture];

    

    //长按手势

    UILongPressGestureRecognizer *longGesture=[[UILongPressGestureRecognizeralloc]initWithTarget:self action:@selector(longGestureAction:)];

    //设置长按时间

    [longGesture setMinimumPressDuration:2];

    [self.view addGestureRecognizer:longGesture];

    

    //旋转手势

    UIRotationGestureRecognizer *rotationGesture =

    [[[UIRotationGestureRecognizer allocinitWithTarget:selfaction:@selector(rotationGestureAction:)] autorelease];

    [viewImg1 addGestureRecognizer:rotationGesture];

    

    //向里或向外捏的手势

    UIPinchGestureRecognizer *twoFingerPinch =

    [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(clickH:)]autorelease];

    [viewImg1 addGestureRecognizer:twoFingerPinch];


}


 


//单击监听

-(void)handleSingleTap:(UITapGestureRecognizer *)singleTap

{

    NSLog(@"单击");

}

//双击监听

-(void)handleDoubleTap:(UITapGestureRecognizer *)doubleTap

{

    NSLog(@"双击");

}



//轻扫监听

-(void)swipeGesture:(UISwipeGestureRecognizer *)swipeGesture

{

    if (swipeGestureRecognizer.direction & UISwipeGestureRecognizerDirectionUp){

        NSLog(@"向上轻扫");

    }

    if (swipeGestureRecognizer.direction & UISwipeGestureRecognizerDirectionDown){

        NSLog(@"向下轻扫");

    }

    if (swipeGestureRecognizer.direction & UISwipeGestureRecognizerDirectionLeft){

        NSLog(@"向左轻扫");

    }

    if (swipeGestureRecognizer.direction & UISwipeGestureRecognizerDirectionRight){

        NSLog(@"向右轻扫");

    }

}


//平移监听

-(void)panGesture:(UIPanGestureRecognizer *)_panGesture

{

    CGPoint point=[panGesture locationInView:self.view];

    viewImg2.center=point;

    NSLog(@"%@",NSStringFromCGPoint(point));

}


//长按监听

-(void)longGestureAction:(UILongPressGestureRecognizer *)longPress

{

    //因为长按超过2秒后放开后会多打印一次,所以此处进行判定在它end状态时return ,不打印

    if (longPress.state==UIGestureRecognizerStateEnded) {

        return;

    }

    NSLog(@"长按");

}


//旋转监听

-(void)rotationGestureAction:(UIRotationGestureRecognizer *)rotation

{

    //将弧度转成角度

    float degree= rotation.rotation*(180/M_PI);

    NSLog(@"%f",degree);

}


//-(void)clickH:(id)sender

//{

//    UIView *view = [[self.view subviews] objectAtIndex:1];

//    [self.view insertSubview:view atIndex:0];

//    //将当前的view放到最底部。

//}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值