【问题标题】:Nested uiscrollviews and custom handling event routing嵌套的 uiscrollviews 和自定义处理事件路由
【发布时间】:2015-04-27 16:07:16
【问题描述】:

我以这样的方式嵌套了 UIScrollViews,即内部 UIScrollView 位于外部 UIScrollView 的第二页。 (即内部框架将是 CGRectMake(320, 0, view.width, view.height))。两个滚动视图仅水平移动。我只想在屏幕上滑动内部滚动视图(即外部滚动视图移动到第二页),直到内部滚动视图到达末尾。一旦内部的到达终点,那么我想识别外部的滑动。我首先尝试在第二页上设置outerScrollView.scrollEnabled = NO,但随后内部也没有收到滑动手势。我还尝试继承 UIScrollView 并覆盖 hitTest:event 以返回内部的 UIView,这也没有成功。有没有办法将滑动事件处理到特定视图并阻止其他视图?

【问题讨论】:

    标签: ios objective-c uiscrollview


    【解决方案1】:

    尝试使用代码here 检测滚动视图何时到达内部视图的底部。如果它到达底部,则翻转一个布尔值(如果用户将滚动视图从底部移开,请记住将其翻转回来!)。如果布尔值被翻转,则使用代码here 将触摸传递给超级视图并允许水平滚动包含的 UIScrollView。如果您在强制滚动时遇到问题,请尝试使用代码 here 强制滚动。

    编辑: 我只是写了一些代码来测试一下。通过限制每个视图可以滚动的方向,我能够让它按照您的描述工作,而无需使用我上面提到的大部分内容(更简单)。这是滚动视图的代码:

    outer = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    [outer setBackgroundColor:[UIColor redColor]];
    [outer setShowsHorizontalScrollIndicator:true];
    [outer setShowsVerticalScrollIndicator:false];
    [outer setScrollEnabled:true];
    [outer setDelegate:self];
    [outer setAlwaysBounceHorizontal:true];
    [outer setAlwaysBounceVertical:false];
    outerContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width*2, self.view.bounds.size.height)];
    [outerContent setBackgroundColor:[UIColor blueColor]];
    [outer addSubview:outerContent];
    outer.contentSize = outerContent.frame.size;
    [self.view addSubview:outer];
    
    inner = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    [inner setBackgroundColor:[UIColor yellowColor]];
    [inner setShowsHorizontalScrollIndicator:false];
    [inner setShowsVerticalScrollIndicator:true];
    [inner setAlwaysBounceHorizontal:false];
    [inner setAlwaysBounceVertical:true];
    [inner setScrollEnabled:true];
    [inner setDelegate:self];
    innerContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height*2)];
    [innerContent setBackgroundColor:[UIColor greenColor]];
    inner.contentSize = innerContent.frame.size;
    [inner addSubview:innerContent];
    
    [self.outerContent addSubview:inner];
    

    我对视图进行了颜色编码,以便您了解发生了什么。 “inner”和“outer”是我在头文件中声明并在类中合成的 UIScrollView。 “innerContent”和“outerContent”也是头文件中的UIView。

    【讨论】:

    • 是的,它应该没有任何并发​​症。我想我在编写测试代码时搞砸了。谢谢你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    相关资源
    最近更新 更多