How to use UIPageControl to create multiple views?

I need to use UIPagecontrol for my application, and I don't know where to start. I'm a newbie, and the examples that Apple gives me are pretty complicated. All I need is 3 pages with different views for each of them.

+10
iphone cocoa-touch uikit
Nov 29 '09 at 17:36
source share
5 answers

Here is a good example from Cocoa With love. It reproduces only 2 actual images, which provides a low level of memory.

+3
Dec 17 '09 at 14:21
source share

You want to use UIScrollView, and then, as a brother, place the UIPageControl above it. Then put each of your pages in a scroll and enable paging. Thus, each β€œclick” will move the scroll one page.

Now designate your view controller as a scroll delegate and view the scrollViewDidEndScrollAnimation file and use contentOffset to determine the current page.

+22
Nov 29 '09 at 18:36
source share

Based on Ben Gottlieb, a pretty perfectly correct answer, I got what I wanted. Here is the code I used for this:

.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]; } 
+15
Sep 14 '11 at 12:36
source share

Just add 3 views to the horizontal scroll view one by one with the paging included in scrollview, and add a UIPageControl control under scrollView. And with both scrollview delegation methods and the pageview action method, you can achieve the basic working UIPageControl

I used contentoffset to find the current page

  // scrollview delegate - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { CGFloat pageWidth = mainScroll.frame.size.width; int page = floor((mainScroll.contentOffset.x - pageWidth / 2) / pageWidth) + 1; pageControl.currentPage=page; } // page view action method - (IBAction)pageControlInteracted:(UIPageControl *)sender { NSLog(@"%d",sender.currentPage); CGRect frame = mainScroll.frame; frame.origin.x = frame.size.width * sender.currentPage; frame.origin.y = 0; [mainScroll scrollRectToVisible:frame animated:YES]; } 
+3
05 Oct '12 at 5:50
source share

See my answer here: Is UIPageControl itself useless?

for a reusable class encapsulating UIPageControl and UIScrollView.

+1
Oct 25 '10 at 12:11
source share



All Articles