UIPanGestureRecognizer手势影响UISlider拖动的问题及解决办法 (转)

最近在使用UISlider控件控制音频播放进度时,遇到一个问题,即:UISlider所在的UIView上已经添加了 UIPanGestureRecognizer类型手势,用来处理向右拖动整个View时取消显示,但这样一来,拖动UISlider就变成了响应手势, 而无法正常使用UISlider。

经过一番查找资料和尝试,问题得到解决,方法如下:

给UIPanGestureRecognizer添加代理(UIGestureRecognizerDelegate),UIGestureRecognizerDelegate中有一个方法:

        -(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch;

在进入手势识别事件之前,先进入该方法,对那些不需要响应手势事件的事件(如拖动UISlider),可以通过返回一个NO型布尔值不响应手势识别;而对于需要响应手势识别的情况,只需要返回一个YES就可以。

下面举例说明:

首先添加手势识别,并给手势识别指定代理

……

   UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:selfaction:@selector(handlePanGesture:)];

    [panRecognizer setMaximumNumberOfTouches:1];

    [panRecognizer setDelaysTouchesBegan:TRUE];

    [panRecognizer setDelaysTouchesEnded:TRUE];

    [panRecognizer setCancelsTouchesInView:TRUE];

   //为手势识别器设置代理

    panRecognizer.delegate = self;

    [self.view addGestureRecognizer:panRecognizer];

    [panRecognizer release];

……

然后,实现UIGestureRecognizerDelegate的方法:

// 当拖动UISlider时会被误认为是手势,所以在这个判断一下

-(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch {

    if([touch.view isKindOfClass:[UISlider class]])

        return NO;

    else

        return YES;

}



----------------------------------------------

uiscrollview


    //手势

    UIPanGestureRecognizer *panGesture =

    [[UIPanGestureRecognizer alloc] initWithTarget:self

                                                  action:@selector(panGesture:)];

    [self.view addGestureRecognizer:panGesture];

    [panGesture release];

   

    self.loginScroll.contentSize=CGSizeMake(675, 254);


- (void)panGesture:(UIPanGestureRecognizer *)gestureRecognizer

{

    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];

   

    //scrollView区域滑动,之后return,跳出方法,页面不滑动。

    if (loginView.hidden == NO && loginScroll.scrollEnabled == YES) {

        CGPoint touchPoint = [gestureRecognizer locationInView:keyWindow];

       

        if (touchPoint.x > 48 && touchPoint.x < 273 && touchPoint.y > 118 && touchPoint.y < 372) {

            return;

        }

    }

   

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {

        touchBeganPoint = [gestureRecognizer locationInView:keyWindow];

        touchLastPoint = touchBeganPoint;

       

    } else if (gestureRecognizer.state == UIGestureRecognizerStateChanged) {

        if (isScroll == YES) {

            return;

        }

       

        CGPoint touchPoint = [gestureRecognizer locationInView:keyWindow];

        CGFloat xOffSet =  touchLastPoint.x - touchPoint.x;

        touchLastPoint = touchPoint;

       

        if(self.navigationController.view.frame.origin.x-xOffSet >= 0)

        {

            return;

        }

       

        if (tableView.scrollEnabled == NO) {

            self.navigationController.view.frame = CGRectMake(self.navigationController.view.frame.origin.x-xOffSet,

                                                             self.navigationController.view.frame.origin.y,

                                                             self.navigationController.view.frame.size.width,

                                                             self.navigationController.view.frame.size.height);

            return;

        }

       

        if (touchBeganPoint.x - touchPoint.x  >= 10) {

            tableView.scrollEnabled = NO;

            loginScroll.scrollEnabled=NO;

        } else if (touchPoint.y - touchBeganPoint.y >= 5 || touchBeganPoint.y - touchPoint.y >= 5) {

            isScroll = YES;

        }

       

    } else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {

       

        [tableView setScrollEnabled:YES];

        if (isScroll == NO) {

            if (touchBeganPoint.x - [gestureRecognizer locationInView:keyWindow].x > kTriggerOffSet)

                [self moveToLeftSide];

            else

                [self restoreViewLocation];

        }

        isScroll = NO;

        self.loginScroll.scrollEnabled=YES;

    }

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值