//
// UITestViewController.m
// UITest
//
#import "UITestViewController.h"
UIScrollView *myScrollView;
UIPageControl *myPageControl;
@implementation UITestViewController
- (void)loadScrollViewWithPage:(UIView *)page
{
int pageCount = [[myScrollView subviews] count];
CGRect bounds = myScrollView.bounds;
bounds.origin.x = bounds.size.width * pageCount;
bounds.origin.y = 0;
page.frame = bounds;
[myScrollView addSubview:page];
}
- (void)createPages
{
CGRect pageRect = myScrollView.frame;
//create pages
UIView *page1 = [[UIView alloc] initWithFrame:pageRect];
page1.backgroundColor = [UIColor blueColor];
UIView *page2 = [[UIView alloc] initWithFrame:pageRect];
page2.backgroundColor = [UIColor redColor];
UIView *page3 = [[UIView alloc] initWithFrame:pageRect];
page3.backgroundColor = [UIColor greenColor];
//add to scrollview
[self loadScrollViewWithPage:page1];
[self loadScrollViewWithPage:page2];
[self loadScrollViewWithPage:page3];
//cleanup
[page1 release];
[page2 release];
[page3 release];
}
- (void)viewDidLoad {
[super viewDidLoad];
float pageControlHeight = 18.0;
int pageCount = 3;
CGRect scrollViewRect = [self.view bounds];
scrollViewRect.size.height -= pageControlHeight;
//create scrollview
myScrollView = [[UIScrollView alloc] initWithFrame:scrollViewRect];
myScrollView.pagingEnabled = YES;
myScrollView.contentSize = CGSizeMake(scrollViewRect.size.width * pageCount,1);
myScrollView.showsHorizontalScrollIndicator = NO;
myScrollView.showsVerticalScrollIndicator = NO;
myScrollView.delegate = self;
//create pageview
CGRect pageViewRect = [self.view bounds];
pageViewRect.size.height = pageControlHeight;
pageViewRect.origin.y = scrollViewRect.size.height;
myPageControl = [[UIPageControl alloc] initWithFrame:pageViewRect];
myPageControl.backgroundColor = [UIColor blackColor];
myPageControl.numberOfPages = pageCount;
myPageControl.currentPage = 0;
[myPageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
//create pages
[self createPages];
//add to main view
[self.view addSubview:myScrollView];
[self.view addSubview:myPageControl];
//cleanup
[myPageControl release];
[myScrollView release];
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
CGFloat pageWidth = sender.frame.size.width;
int page = floor((sender.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
myPageControl.currentPage = page;
}
- (void)changePage:(id)sender
{
int page = myPageControl.currentPage;
// update the scroll view to the appropriate page
CGRect frame = myScrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[myScrollView scrollRectToVisible:frame animated:YES];
}
- (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.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
转载于:https://www.cnblogs.com/lipeng930307/archive/2012/12/18/PageContro.html