简体   繁体   English

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

[英]How to interact with a horizontal scrollview that is a subview of a scroll view?

I have a horizontal scroll view that is a subview of a vertical scroll view. 我有一个水平滚动视图,它是垂直滚动视图的子视图。 It looks like the vertical scroll view is intercepting all user interactions. 看起来垂直滚动视图正在拦截所有用户交互。 I want to be able to scroll horizontally the horizontal scroll view. 我希望能够水平滚动水平滚动视图。 How would I do this? 我该怎么做?

Here is a short snippet of my code: 这是我的代码的简短片段:

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

You can subclass the parent scrollview and override touchesShouldCancelInContentView: to return NO whenever the view is child scroll view. 您可以子类化父滚动视图并覆盖touchesShouldCancelInContentView:每当视图是子滚动视图时返回NO。 ie; 即;

MyScrollView.h

@interface MyScrollView: UIScrollView
@end

and in MyScrollView.m 并在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];
      }
}

Then your posted code should look like: 然后您发布的代码应如下所示:

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

Unable to comment. 无法发表评论。 Hence answering here. 因此回答这里。

I faced this problem earlier. 我之前遇到过这个问题。 I do not remember the exact solution. 我不记得确切的解决方案。 But I think the problem was that you are trying to add the second view controller after the second. 但我认为问题是你试图在第二个之后添加第二个视图控制器。 I think you should try the reverse. 我想你应该反过来试试。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM