簡體   English   中英

如何與作為滾動視圖子視圖的水平滾動視圖進行交互?

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

我有一個水平滾動視圖,它是垂直滾動視圖的子視圖。 看起來垂直滾動視圖正在攔截所有用戶交互。 我希望能夠水平滾動水平滾動視圖。 我該怎么做?

這是我的代碼的簡短片段:

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

您可以子類化父滾動視圖並覆蓋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];

無法發表評論。 因此回答這里。

我之前遇到過這個問題。 我不記得確切的解決方案。 但我認為問題是你試圖在第二個之后添加第二個視圖控制器。 我想你應該反過來試試。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM