2012-08-07 106 views
0

我是iphone開發新手。任何人都可以請告訴我如何在水平滾動視圖內添加垂直滾動視圖。我經歷了很多樣本​​,但無法獲得清晰的圖片。我希望我的視角能夠在垂直和水平方向滾動。任何幫助表示讚賞。iPhone中的嵌套滾動視圖

這裏是我的滾動代碼:

編輯:這裏是我的滾動

self.firstScroll.pagingEnabled=YES; 

self.firstScroll.clipsToBounds=YES; 

    int numberOfViews=3; 

for(int i=0;i<numberOfViews;i++){ 

    CGFloat xOrigin=self.view.frame.size.width*i; 

    UIView *awesomeView=[[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0,  self.view.frame.size.width, self.view.frame.size.height)]; 
    awesomeView.backgroundColor=[UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1]; 
    [self.firstScroll addSubview:awesomeView]; 
} 
self.firstScroll.contentSize=CGSizeMake(self.view.frame.size.width*numberOfViews,  self.view.frame.size.height); 
[self.view addSubview:firstScroll]; 


self.nextVerticalScroll.pagingEnabled=YES; 
for(int i=0;i<numberOfViews ;i++){ 
    CGFloat yOrigin=self.view.frame.size.height * i; 
    UIView *verticalView=[[UIView alloc]initWithFrame:CGRectMake(0, yOrigin, self.view.frame.size.width, self.view.frame.size.height)]; 
    verticalView.backgroundColor=[UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1]; 
    [self.nextVerticalScroll addSubview:verticalView]; 
} 
self.nextVerticalScroll.contentSize=CGSizeMake(self.view.frame.size.width, self.view.frame.size.height*numberOfViews); 
[self.view addSubview:nextVerticalScroll]; 

感謝代碼,

拉吉

+3

你到目前爲止嘗試過什麼?你有沒有做過任何測試?如創建一個滾動視圖,然後roatate它,然後在旋轉的滾動視圖內添加另一個滾動視圖,或投擲您的計算機... somthing ... – doNotCheckMyBlog 2012-08-07 12:55:49

+0

嗨,我已經把我的代碼在編輯 – Raj 2012-08-07 13:04:59

+2

'UIScrollView'允許滾動在兩個方向...你爲什麼使用兩個? – Dustin 2012-08-07 14:06:47

回答

1

你可能不希望有內scrollviews滾動視圖,因爲你無法控制哪些獲得水平手勢,哪些獲得垂直手勢(至少沒有太多麻煩)。這是很容易有一個contentSize是更大的橫向和縱向比滾動視圖本身的bounds,單滾動視圖如:

- (void)configureScrollView 
{ 
    NSInteger rows = 4; 
    NSInteger cols = 5; 

    CGFloat width = self.scrollView.bounds.size.width; 
    CGFloat height = self.scrollView.bounds.size.height; 

    // configure my scroll view itself 

    self.scrollView.contentSize = CGSizeMake(width * cols, height * rows); 
    self.scrollView.pagingEnabled = YES; 
    self.scrollView.backgroundColor = [UIColor darkGrayColor]; 

    // now let's add the labels to the scrollview 

    for (NSInteger row = 0; row < rows; row++) 
    { 
     for (NSInteger col = 0; col < cols; col++) 
     { 
      // making my label just a little smaller than the scrollview's bounds so I can easily see the scrolling/paging 

      CGRect frame = CGRectMake((width * col) + 20.0, (height * row) + 20.0, width - 40.0, height - 40.0); 

      // create and configure the label 

      UILabel *label = [[UILabel alloc] initWithFrame:frame]; 
      label.text = [NSString stringWithFormat:@"%1.0f", row * cols + col + 1.0]; 
      label.textAlignment = UITextAlignmentCenter; 
      label.backgroundColor = [UIColor lightGrayColor]; 

      // add it to my scrollview 

      [self.scrollView addSubview:label]; 
     } 
    } 
} 

我只是填補我用20個不同的文字標籤滾動視圖(和將scrollview的背景與標籤區分開來,這樣我就可以看到它們),但是它展示了基本的想法。

+0

感謝瑞恩你讓我的一天:) – Raj 2012-08-08 05:15:25

0

如果您在XIB中添加了Paging,請將其刪除...我認爲這會對您有所幫助。