【问题标题】:Xcode, UIscrollView and paginationXcode、UIscrollView 和分页
【发布时间】:2016-07-06 19:29:03
【问题描述】:

我是初学者,我需要知道如何在一个 UIScrollView 中放入多个页面。这些页面应该包含交互元素,例如按钮、视频以及文本和图像。我将不胜感激任何指向教程的链接或您能给我的一些线索。

问候。

【问题讨论】:

    标签: ios uiscrollview pagination


    【解决方案1】:
    1. 将滚动视图的pagingEnabled 属性设置为YES
    2. 如果您想要水平分页,请将滚动视图的 contentSizeproperty X * width 设为宽,如果您想要垂直分页,则设为“X * height”高。
    3. 通过为每个页面添加正确的偏移量(`X * width' 或 'X * height' 取决于水平/垂直),将子视图添加到每个“页面”。ž

    X 是页数,从 0 开始。

    这是一个有 5 个水平页面的示例。

    int numberOfPages = 5;
    UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
    someScrollView.pagingEnabled = YES;
    someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
    [self.view addSubview:someScrollView];
    [someScrollView release];
    
    for (int i = 0; i < numberOfPages; i++) {
        UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * someScrollView.frame.size.width + 20,
                                                                      20,
                                                                      someScrollView.frame.size.width - 40,
                                                                      20)];
        tmpLabel.textAlignment = UITextAlignmentCenter;
        tmpLabel.text = [NSString stringWithFormat:@"This is page %d", i];
        [someScrollView addSubview:tmpLabel];
        [tmpLabel release];
    }
    

    【讨论】:

    • 感谢您的回答,我很有用。
    【解决方案2】:

    听起来页面控件就是您要找的那个。

    这里是示例代码的链接:LiNk

    【讨论】:

      【解决方案3】:

      您需要在 UIScrollView 上设置 2 个属性,以便获得平滑的分页滚动。

      [scroller setPagingEnabled:YES];
      [scroller setContentSize:CGSizeMake(width, height)];
      /* width here would be your view's width times the amount of pages you want. */
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-17
        • 2023-04-04
        相关资源
        最近更新 更多