简体   繁体   English

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

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

I have a bit of a complex situation involving multiple gestures. 我有一个涉及多个手势的复杂情况。 Basically, I want to have one container UIScrollView that only scrolls left-to-right if the touches are within a specific area. 基本上,我希望有一个容器UIScrollView,如果触摸在特定区域内,则只能从左向右滚动。 If they are not within the area, the UIScrollView passes those touches on to child UIViews that exist side-by-side inside the UIScrollView (think of it like a panel navigation). 如果它们不在该区域之内,则UIScrollView会将这些触摸传递到在UIScrollView内并排存在的子UIViews(将其视为面板导航)。

I have the UIScrollView containing UIViews working fine. 我有包含UIViews的UIScrollView正常工作。 I subclassed UIScrollView and added the panning restriction via TouchesBegan/TouchesMoved/TouchesEnded/TouchesCancelled. 我继承了UIScrollView的子类,并通过TouchesBegan / TouchesMoved / TouchesEnded / TouchesCancelled添加了平移限制。 Everything works except when the UIView is a UITableView. 一切正常,除了UIView是UITableView时。 It seems at that point my parent UIScrollView never gets these events and so can never restrict the panning properly. 似乎在那时,我的父UIScrollView从未收到这些事件,因此也永远无法正确限制平移。

Anyone have any ideas for how to accomplish this? 有人对如何实现此目标有任何想法吗?

Thanks! 谢谢!

The way to do this is to subclass the subviews that are eating the touch events and not allowing the UIScrollView to get them. 执行此操作的方法是将正在吃触摸事件的子视图子类化,并且不允许UIScrollView获取它们。 Then, override the pointInside: method (with the appropriate exception for UI that you want to still work). 然后,重写pointInside:方法(具有您要仍然使用的UI的适当例外)。 For example: 例如:

- (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