【问题标题】:nested scrollview in iphoneiphone中的嵌套滚动视图
【发布时间】:2012-08-07 12:52:53
【问题描述】:

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

谢谢,

拉吉

【问题讨论】:

  • 到目前为止您尝试过什么?你做过什么测试吗?比如创建一个滚动视图,然后旋转它,然后在旋转的滚动视图中添加另一个滚动视图,或者扔你的电脑……某事……
  • 您好,我已将代码放入 EDIT 中
  • UIScrollView 允许双向滚动...为什么要使用两个?
  • 你能详细告诉我如何做到这一点。很抱歉缺乏知识
  • 现在您将 contentSize 设置为具有self.view.frame.size.width 的宽度。如果您将其设置为 self.view.frame.size.width * numberOfViewsHorizontal 或类似的,您也将获得水平滚动。

标签: iphone objective-c xcode ipad ios5


【解决方案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 个不同的文本标签填充我的滚动视图(并且滚动视图的背景颜色与标签不同,以便我可以看到它们),但它展示了基本思想。

【讨论】:

  • 感谢瑞恩你让我开心:)
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多