0

我有一个水平滚动视图,它是垂直滚动视图的子视图。看起来垂直滚动视图正在拦截所有用户交互。我希望能够水平滚动水平滚动视图。我该怎么做?

这是我的代码的一小段:

  UIScrollView *scrollView2 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,     self.view.width, 100)];

  scrollView2.contentSize = CGSizeMake(self.view.bounds.size.width + 400, 100);

  _scrollView.contentSize = CGSizeMake(self.view.width, 400);

  _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.height - 108)];

[self.view addSubview:_scrollView];

[_scrollView addSubview:scrollView2];
4

2 回答 2

2

您可以子类化父滚动视图并覆盖touchesShouldCancelInContentView:以在视图是子滚动视图时返回 NO。IE;

MyScrollView.h

@interface MyScrollView: UIScrollView
@end

在 MyScrollView.m

@implementation MyScrollView


-(BOOL)touchesShouldCancelInContentView:(UIView *)touchedView{
      if(touchedView == _scrollView2){ //Get the reference of that child scroll view here
         return NO;
      }else{
         [super touchesShouldCancelInContentView:touchedView];
      }
}

然后您发布的代码应如下所示:

UIScrollView *scrollView2 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,     self.view.width, 100)];

  scrollView2.contentSize = CGSizeMake(self.view.bounds.size.width + 400, 100);



  _myScrollView = [[MyScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.height - 108)];
  _myScrollView.contentSize = CGSizeMake(self.view.width, 400);


[_myScrollView addSubview:scrollView2];
[self.view addSubview:_myScrollView];
于 2013-03-16T07:01:13.430 回答
0

无法发表评论。因此在这里回答。

我之前遇到过这个问题。我不记得确切的解决方案。但我认为问题在于您试图在第二个视图控制器之后添加第二个视图控制器。我认为你应该尝试反过来。

[_scrollView addSubview:scrollView2];
[self.view addSubview:_scrollView];
于 2013-03-16T06:43:57.400 回答