How to combine UIScrollview with UIPagecontrol to show different views?

前端 未结 4 1317
刺人心
刺人心 2021-02-06 14:59

I\'ve searched and searched for a tutorial for this but none of them are what I\'m looking for. I\'ve tried Apple\'s sample but it is just colors and I don\'t know how to make i

4条回答
  •  鱼传尺愫
    2021-02-06 15:50

    I was experimenting with this just the other day. I'm still getting used to using a UIScrollView but here's how you can add views to your UIScrollView:

    UIView *blueView = [[UIView alloc] init];
    blueView.frame = CGRectMake(100, 0, 500, 1024);
    blueView.backgroundColor = [UIColor colorWithRed:164.0/256 green:176.0/256 blue:224.0/256 alpha:1];
    [scrollView addSubview:blueView];
    [blueView release];
    
    UIView *orangeView = [[UIView alloc] init];
    orangeView.frame = CGRectMake(700, 0, 500, 1024);
    orangeView.backgroundColor = [UIColor colorWithRed:252.0/256 green:196.0/256 blue:131.1/256 alpha:1];
    [scrollView addSubview:orangeView];
    [orangeView release];
    

    Notice that I'm setting the x value in frame.origin of each view so that they're sitting adjacent to each other. You also have to set the content size of the UIScrollView with something like [scrollView setContentSize:CGSizeMake(1200, 1024)]; so that it knows how big its subviews are.

    Then, if you need to control a UIPageControl, you would set its numberOfPages to 2 (for the example scrollview above) and change its currentPage property. You could do this by implementing scrollViewDidEndDecelerating:, which is a method in the UIScrollViewDelegate. You could check which "page" the scrollview is at by checking its contentOffset.x value.

    Hope this helps!

提交回复
热议问题