This is what I have so far for the pageControl. The only issue is that I need to specify what's loaded on a page in 2 places - I'm not sure if I'm doing this correctly. Right now it only works if I use the pageControl "buttons" at the bottom of the screen, swiping to the page doesn't do anything
.m file
Code:
static int kNumberOfPages = 2;
#import "MainViewViewController.h"
@implementation MainViewViewController
@synthesize scrollView, pageControl;
//Make sure the pageControl corresponds to the current page
-(void)scrollViewDidScroll:(UIScrollView *)sender{
if(!pageControlUsed){
//Switch the indicator when more than 50% of the previous or next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
if(page == 0){
NSLog(@"its zero");
} else if (page == 1){
NSLog(@"its one");
}
[self loadPages];
}
}
-(void)loadPages
{
if (pageControl.currentPage == 1){
NSLog(@"1");
}
}
-(void)pageTurning:(UIPageControl *)pageController
{
NSInteger nextPage = [pageController currentPage];
switch (nextPage){
case 0:
scrollView.backgroundColor = [UIColor yellowColor];
break;
case 1:
scrollView.backgroundColor = [UIColor blueColor];
break;
default:
break;
}
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
pageControlUsed = NO;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
pageControlUsed = NO;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
pageControl.currentPage = 1;
[pageControl addTarget:self action:@selector(pageTurning:) forControlEvents:UIControlEventValueChanged];
pageControlUsed = NO;
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * kNumberOfPages, self.scrollView.frame.size.height);
scrollView.delegate = self;
//Control pageControl
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = kNumberOfPages;
// Do any additional setup after loading the view from its nib.
}
.h file
Code:
#import <UIKit/UIKit.h>
@interface MainViewViewController : UIViewController <UIScrollViewDelegate>
{
IBOutlet UIScrollView *scrollView;
IBOutlet UIPageControl *pageControl;
BOOL pageControlUsed;
}
@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UIPageControl *pageControl;