繁体   English   中英

将UITableView和其他UIView嵌入受限制的UIScrollView中

[英]Embed UITableView and other UIViews inside a restricted UIScrollView

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

我有包含UIViews的UIScrollView正常工作。 我继承了UIScrollView的子类,并通过TouchesBegan / TouchesMoved / TouchesEnded / TouchesCancelled添加了平移限制。 一切正常,除了UIView是UITableView时。 似乎在那时,我的父UIScrollView从未收到这些事件,因此也永远无法正确限制平移。

有人对如何实现此目标有任何想法吗?

谢谢!

执行此操作的方法是将正在吃触摸事件的子视图子类化,并且不允许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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM