ios - 嵌套的 UIScrollViews 和事件路由

标签 ios uiscrollview nested hittest touchesbegan

我有 2 个 ScrollView ,它们都应该垂直滚动。外部 ScrollView (红色)包含一个搜索栏和内部 ScrollView (蓝色)。内部 ScrollView 应该可以无限滚动(它包含图像/项目并具有无限滚动实现)。

我希望这个 Controller 的工作方式如下:

当我向下滚动时,外部 ScrollView 应该首先滚动,搜索栏应该消失(滚动出内容区域)。只有在那之后,内部 ScrollView 才应该开始滚动。 向上滚动时,内部 ScrollView 应该一直滚动到顶部。只有这样,外部 ScrollView 才应接收滚动事件并最终向上滚动以使搜索栏再次可见。

如果我只是将它们嵌套在 IB 中而不进行任何修改,则内部 ScrollView 会捕获所有滚动事件并且它会以相反的方式工作。

请记住,我在这里使用内部 ScrollView 作为简化的比喻。在我的应用程序中,我实际上在这里有一个控件,它有一个带有嵌套表格 View 的 ScrollView ( ScrollView 让我水平翻页,表格 View 让我垂直滚动)。

enter image description here

最佳答案

如果可以,在 2 个 ScrollView 上设置一个通用的 UIScrollViewDelegate,并实现以下内容:

- (void) scrollViewDidScroll: (UIScrollView*) scrollView
{
  if (scrollView == self.mainScrollView)
  {
    /* Handle instances when the main scroll view is already at the bottom */
    if (   scrollView.contentOffset.y
        == scrollView.contentSize.height - scrollView.bounds.size.height)
    {
      /* Stop scrolling the main scroll view and start scrolling the
       * inner scroll view
       */
      self.innerScrollView.scrollEnabled = YES;
      self.mainScrollView.scrollEnabled = NO;
    }
    else
    {
      /* Start scrolling the main scroll view and stop scrolling the
       * inner scroll view
       */
      self.innerScrollView.scrollEnabled = NO;
      self.mainScrollView.scrollEnabled = YES;
    }
  }
  else if (scrollView == self.innerScrollView)
  {
    /* Handle instances when the inner scroll view is already at the top */
    if (self.innerScrollView.contentOffset.y == 0)
    {
      /* Stop scrolling the inner scroll view and start scrolling the
       * main scroll view
       */
      self.innerScrollView.scrollEnabled = NO;
      self.mainScrollView.scrollEnabled = YES;
    }
    else
    {
      /* Start scrolling the inner scroll view and stop scrolling the
       * main scroll view
       */
      self.innerScrollView.scrollEnabled = YES;
      self.mainScrollView.scrollEnabled = NO;
    }
  }
}

请注意,我还没有对此进行测试,但逻辑可能有点像这样(您可以设置滚动启用或禁用用户交互,或其他)。这很可能不是您想要的解决方案,但我确信普通的 UIScrollViewDelegate 是解决您问题的方法。

关于ios - 嵌套的 UIScrollViews 和事件路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16103492/

相关文章:

regex - 正则表达式可以处理嵌套操作吗?

iphone - cell.titleLabel的字体设置应该在cellForRowAtIndexPath:方法中的什么位置?

ios - 使用 ReactiveCocoa 4 在 MVVM 中将信号从 View 传递到模型

ios - 使用滑动手势关闭键盘

haskell - Haskell 中的嵌套列表追加

list - Scheme - 映射函数,用于将函数应用于嵌套列表中的元素

ios - 如果我不小心覆盖了 Apple 的私有(private) API 会怎样?

ios - flutter ITMS-90809 : Deprecated API Usage - Apple will stop accepting submissions of apps that use UIWebView APIs

ios - 创建 iOS 可滚动时间轴 View

ios - 在 UIScrollView 的 Paging 上重用 3 个 View