Xcode, UIscrollView and pagination

2019-02-16 05:29发布

I am a beginner, I need to know how I can put in a UIScrollView multiple pages. These pages should contain interactive elements such as buttons,video and also text and images. I would appreciate any link to a tutorial or some clue that you could give me.

Regards.

3条回答
叛逆
2楼-- · 2019-02-16 05:40
  1. set your scroll view's pagingEnabled property to YES
  2. make your scroll view's contentSizeproperty X * width wide if you want horizontal paging or `X * height' tall if you want vertical paging.
  3. add subview to each "page" by adding them with the right offset for each page (`X * width' or 'X * height' depending on horizontal/vertical).ž

X is a number of pages, starting with 0.

Here is a sample with 5 horizontal pages.

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];
}
查看更多
不美不萌又怎样
3楼-- · 2019-02-16 05:53

Sounds like page control is the one that you are looking for.

Here is the link with sample code: LiNk

查看更多
够拽才男人
4楼-- · 2019-02-16 06:01

You need to set 2 properties on your UIScrollView in order to have a smooth paging scroll.

[scroller setPagingEnabled:YES];
[scroller setContentSize:CGSizeMake(width, height)];
/* width here would be your view's width times the amount of pages you want. */
查看更多
登录 后发表回答