0

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

谢谢,

拉吉

4

2 回答 2

1

您可能不希望在滚动视图中有滚动视图,因为您无法控制哪个获得水平手势和哪个获得垂直手势(至少没有很多麻烦)。拥有一个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 个不同的文本标签填充我的滚动视图(并且滚动视图的背景与标签的颜色不同,因此我可以看到它们),但它展示了基本思想。

于 2012-08-07T15:14:05.163 回答
0

如果您在 XIB 中添加了分页,请删除它...我认为它会对您有所帮助。

于 2013-08-31T12:09:27.230 回答