PageContro

//
// 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值