在受限制的UIScrollView中嵌入UITableView和其他UIViews

时间:2014-05-04 19:27:29

标签: ios objective-c uitableview uiscrollview

我有一些涉及多个手势的复杂情况。基本上,我想有一个容器UIScrollView,只有触摸在特定区域内时才从左向右滚动。如果它们不在该区域内,则UIScrollView将这些触摸传递给UIScrollView内并排存在的子UIViews(将其视为面板导航)。

我有包含UIViews的UIScrollView正常工作。我将UIScrollView子类化,并通过TouchesBegan / TouchesMoved / TouchesEnded / TouchesCancelled添加了平移限制。除非UIView是UITableView,否则一切正常。在这一点上,我的父UIScrollView似乎永远不会获得这些事件,所以永远不能正确地限制平移。

任何人对如何做到这一点都有任何想法?

谢谢!

1 个答案:

答案 0 :(得分:1)

这样做的方法是子类化正在吃触摸事件的子视图,而不允许UIScrollView获取它们。然后,覆盖pointInside:方法(对于您仍想使用的UI,有适当的例外)。例如:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
// Confine the offending control to a certain area
CGRect frame = CGRectMake(0, 0,
                          self.frame.size.width,
                          self.frame.size.height - 100.00);

// Except for subview buttons (or some other UI element)
if([self depthFirstButtonTest:self pointInside:point withEvent:event])
{
    return YES;
}

return (CGRectContainsPoint(frame, point));
}

- (BOOL)depthFirstButtonTest:(UIView*)view pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
for (UIView * subview in view.subviews)
{
    if([self depthFirstButtonTest:subview pointInside:point withEvent:event])
    {
        return YES;
    }
}

// Is it a button? If so, perform normal testing on it
if ([view isKindOfClass:[UIButton class]]) {
    CGPoint pointInButton = [view convertPoint:point fromView:self];
    if ([view pointInside:pointInButton withEvent:event]) {
        return YES;
    }
}

return NO;
}