【问题标题】:Is it possible to pass the scroll event to the right nested UIScrollview?是否可以将滚动事件传递给右侧嵌套的 UIScrollview?
【发布时间】:2013-11-04 16:18:27
【问题描述】:

我有两个带有 PagingEnabled 的水平 UIScrollView。

在纵向模式下,一切正常,但在横向模式下,滚动视图之间出现冲突。例如,如果当前可见视图是 ScrollView2.View2 并且我正在滚动到 ScrollView1.View3,则 ScrollView2 滚动以及 ScrollView1。它以某种方式接收 ScrollView1 的滚动事件。结果我得到的 ScrollView2.contentOffset 等于 0.0(但它应该等于 View2 的 X,例如 384.0)。

是否可以确定哪个滚动条正在滚动?我尝试使用 UIScrollViewDelegate 方法进行修复,但没有帮助我,如果我使用 WebViews 而不是 Views,情况会变得更糟。

编辑:I have added a small sample to github

正如我之前提到的,我尝试在“didScroll”和其他委托方法中检查滚动视图的实例,但是在这些方法中同步所有内容并不容易。而且我试图覆盖 hitTest 方法,也没有帮助我。

【问题讨论】:

    标签: ios objective-c uiscrollview


    【解决方案1】:

    scrollViewDidScroll:

    当用户滚动 接收器内的内容视图。

    • (void)scrollViewDidScroll:(UIScrollView *)scrollView

    这样有什么问题吗?

    -(void)scrollViewDidScroll:(UIScrollView*)scrollView
    {
        if(scrollView == ScrollView2)
        {
            // do stuff
        }
    }
    

    【讨论】:

      【解决方案2】:

      我还没有找到将事件传递给正确滚动视图的方法。但这里有几件事使它起作用:

      首先,你需要在所有嵌套的 UIScrollView 中禁用 Paging Enabled(因为嵌套的 UIScrollView 会随着父 UIScrollView 滚动)。

      实现嵌套滚动视图的分页:

      /*
       * User stopped dragging the innerScroll, the view is not decelerating 
       * and it is still not at its place. Lets help the view to get into right place.
       */
      -(void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
          if ([scrollView isEqual:innerScroll] && !decelerate) {
              if(scrollView.contentOffset.x <= view1.frame.size.width/2){
                  [innerScroll scrollRectToVisible:view1.frame animated:YES];
              } else {
                  [innerScroll scrollRectToVisible:view2.frame animated:YES];
              }
          }
      }
      
      /*
       * User stopped dragging the innerScroll and the View is decelerating. 
       * Lets skip an efforts of the View to get into right place and put it ourselves.
       */
      - (void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
          if ([scrollView isEqual:innerScroll]) {
              if(scrollView.contentOffset.x <= view1.frame.size.width/2){
                  [innerScroll scrollRectToVisible:view1.frame animated:YES];
              } else {
                  [innerScroll scrollRectToVisible:view2.frame animated:YES];
              }
          }
      }
      

      当你内部的 UIScrollView 没有到达最后一个视图时,禁用和启用父 UIScrollViews。

      -(void)scrollViewDidScroll:(UIScrollView *)scrollView {
          if([scrollView isEqual:innerScroll]){
              if(CGRectIntersectsRect(scrollView.bounds, view1.frame)){
                  if(CGRectIntersectsRect(filterScroll.bounds, view11.frame)){
      
                  } else if(CGRectIntersectsRect(filterScroll.bounds, view22.frame)){
      
                  }
                  mainScroll.scrollEnabled = NO;
              } else if (CGRectIntersectsRect(scrollView.bounds, view2.frame)){
                  mainScroll.scrollEnabled = YES;
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-04-13
        • 1970-01-01
        • 1970-01-01
        • 2010-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-06
        • 1970-01-01
        相关资源
        最近更新 更多