禁用UIScrollView将事件拖动到特定方向并将事件发送到super

时间:2013-09-09 18:04:26

标签: ios objective-c uitableview uiscrollview uievent

我有UIView并且我附加了UIPanGestureRecognizer。 在UIView里面,我UIScrollView启用了分页,我设置了内容大小,因此scrollView可以向左滚动

问题:

我希望当用户尝试拖动scrollView时,将事件发送到UIView,以便“UIPanGestureRecognizer”可以处理触摸事件

3 个答案:

答案 0 :(得分:3)

最后,经过6个小时,我弄清楚了 我将UIScrollView子类化并实施gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer委托方法

当用户拖动scrollView时,此方法被调用(BOOL)gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer,默认情况下返回NO

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    if (self.contentOffset.x > 0 &&  self.contentOffset.x <= self.contentSize.width - self.frame.size.width) {
        //user is in middle of dragging the scrollView don't allow any other gestureRecognizer to respond
        return NO;
    }else{
        //scrollView contentOffset is 0 and user is not dragging the scrollView so let other gestureRecognizer to respond
        return YES;
    }
}

答案 1 :(得分:1)

在尝试使用homam的答案时,我意识到在scrollView.panGestureRecognizer之后return NO被切断了。我想要的是我的自定义平移识别器被切断,scrollView仍然滚动。我认为这可能是手势被添加到不同级别的结果,我不确定。

但是,我找到了一个解决方案,通过更改我的自定义手势识别器来检查scrollView.contentOffset并且不激活,除非它大于零。一旦scrollView.bounce变为零,我也关闭了scrollView.contentOffset属性。向下滚动我的scrollView时会触及结束,我的手势识别器会立即接管并将超级视图移开,就像我想要的那样。

这是我自定义平移手势的骨架

- (void)customPan:(UIPanGestureRecognizer*)panRecognizer
{
    if(panRecognizer.state == UIGestureRecognizerStateBegan)
    {
        //do begin stuff
    }

    if(panRecognizer.state == UIGestureRecognizerStateChanged)
    {
        if(self.scrollView.contentOffset.y > 0)
        {
            //do begin stuff to make sure it’s initialized properly when it does start to change
            self.scrollView.bounces = YES;
            return;
        }
        else
        {
            self.scrollView.bounces = NO;
        }

        //do change stuff
    }

    if(panRecognizer.state == UIGestureRecognizerStateEnded)
    {
        if(self.scrollView.contentOffset.y > 0)
        {
            //do begin stuff to make sure it’s initialized properly when it does start to change
            self.scrollView.bounces = YES;
            return;
        }
        else
        {
            self.scrollView.bounces = NO;
        }

        //do end stuff

    }
}

答案 2 :(得分:0)

[myScrollView setContentSize:CGSizeZero];