Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use UIPageControl to create multiple views?

I need to use UIPagecontrol for my application, and I'm not sure how to get started. I'm a beginner, and the examples that apple gives me are pretty complicated. All I need are 3 pages with different views for each of them.

like image 353
lab12 Avatar asked Nov 29 '09 17:11

lab12


2 Answers

You'll want to use a UIScrollView, and then, as a sibling, position the UIPageControl over it. Then put each of your pages into the scroll view and turn paging on for it. This way each 'flick' will move the scroll view one page over.

Now, assign your view controller to be the delegate of the scroll view, and watch for scrollViewDidEndScrollAnimation, and use the contentOffset to determine which page is current.

like image 124
Ben Gottlieb Avatar answered Sep 28 '22 06:09

Ben Gottlieb


Based on Ben Gottlieb's quite excellently correct answer there, I got what I wanted. Here's the code I used to accomplish it:

.h

@interface MyViewController : UIViewController <UIScrollViewDelegate> {
}
@property (nonatomic, retain) UIPageControl *helpPageCon;

.m

@synthesize helpPageCon;

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGRect frame = [[UIScreen mainScreen] applicationFrame];
    float roundedValue = round(scrollView.contentOffset.x / frame.size.width);
    self.helpPageCon.currentPage = roundedValue;    
}

- (void)viewDidLoad
{
    UIScrollView *scrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height-50)] autorelease];
    scrollView.contentSize = CGSizeMake(frame.size.width*5, frame.size.height-50);
    [scrollView setPagingEnabled:YES];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];

    CGRect frame = [[UIScreen mainScreen] applicationFrame];
    self.helpPageCon = [[[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 50)] autorelease];
    self.helpPageCon.center = CGPointMake(frame.size.width/2, frame.size.height-75);
    self.helpPageCon.numberOfPages = 5;
    [self.view addSubview:self.helpPageCon];
}
- (void)dealloc
{
    [super dealloc];
    [helpPageCon release];
}
like image 43
Derek Bredensteiner Avatar answered Sep 28 '22 07:09

Derek Bredensteiner