如何与作为滚动视图子视图的水平滚动视图进行交互?

时间:2013-03-16 03:15:39

标签: ios cocoa-touch uiscrollview uigesturerecognizer

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

以下是我的代码的简短摘录:

  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];

2 个答案:

答案 0 :(得分:2)

只要视图是子滚动视图,您就可以子类化父滚动视图并覆盖touchesShouldCancelInContentView:以返回NO。即;

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];

答案 1 :(得分:0)

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

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

[_scrollView addSubview:scrollView2];
[self.view addSubview:_scrollView];