//UIPageControl主要用来控制分页, 通常和UIScrollView一起使用
// UIPageControl *firstPageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
//
//
// firstPageControl.backgroundColor = [UIColor orangeColor];
//
// [self.view addSubview:firstPageControl];
//
// [firstPageControl release];
//
// //设置页数
// firstPageControl.numberOfPages = 5;
//
// //设置当前为第几页 属性: currentPage(默认 0(第一页))
// firstPageControl.currentPage = 1;
//
// //设置除了当前点的其他点的背景颜色
// firstPageControl.pageIndicatorTintColor = [UIColor greenColor];
//
//
// //设置选中的点的背景颜色
// firstPageControl.currentPageIndicatorTintColor = [UIColor blackColor];
//
self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 200, self.view.frame.size.height - 150, 200, 50)];
self.page.backgroundColor = [UIColor clearColor];
self.page.currentPage = 1;
self.page.currentPageIndicatorTintColor = [UIColor whiteColor];
self.page.pageIndicatorTintColor = [UIColor grayColor];
self.page.numberOfPages = 4;
//添加事件, 实现点击pageControl, 使ScrollView变换图片
[self.page addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_page];
[self.view bringSubviewToFront:_page];
UIScrollView *images = [[UIScrollView alloc]initWithFrame:self.view.frame];
images.contentSize = CGSizeMake(self.view.frame.size.width * 4, self.view.frame.size.height);
for (int i = 0; i < 4; i++) {
UIImageView *picture = [[UIImageView alloc]initWithFrame:CGRectMake(images.frame.size.width * i, 0, images.frame.size.width, images.frame.size.height)];
picture.image = [UIImage imageNamed:[NSString stringWithFormat:@"bigbang%d.jpg", i + 1]];
[images addSubview:picture];
}
images.pagingEnabled = YES;
images.delegate = self;
images.tag = 100;
[self.view addSubview:images];
[self.view sendSubviewToBack:images];
[images release];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(scrollView.frame.size.width - 150, scrollView.frame.size.height - 100, 100, 50)];
//
// pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
//
// pageControl.backgroundColor = [UIColor orangeColor];
//
// pageControl.numberOfPages = 4;
//
// int temp = scrollView.contentOffset.x / scrollView.frame.size.width;
//
// pageControl.currentPage = temp;
//
// pageControl.pageIndicatorTintColor = [UIColor grayColor];
//
//
// [self.view addSubview:pageControl];
//
// [pageControl release];
self.page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;
//[self.view bringSubviewToFront:_page];
}
-(void)pageControlAction:(UIPageControl *)pageControl
{
UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:100];
scrollView.contentOffset = CGPointMake(self.view.frame.size.width * pageControl.currentPage, 0);
}