用户点击页面控件,会触发UIControlEventValueChanged事件,并启动设置为控件动作的任何方法。可以通过调用currentPage查询控件的新值,并通过调整numberOfPages属性设置可用的页面数。
// 初始化页面控件
pageControl.numberOfPages = 9;
pageControl.currentPage = 0;
[pageControl addTarget:self action:@selector(pageTurn) forControlEvents:UIControlEventValueChanged];
- (void)pageTurn:(UIPageControl *)pageControl
{
CATransition *transition;
int secondPage = [pageControl currentPage];
if((secondPage - currentPage)>0)
transition = [self getAnimation:@"fromRight"];
else
transition = [self getAnimation:@"fromLeft"];
UIImageView *newView = (UIImageView *)[[contentView subviews] objectAtIndex:0];
[newView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"ipad_wallpaperd.jpg",secondPage+1]]];
[contentView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[[contentView layer] addAnimation:transition forKey:@"transitionView Animation"];
currentPage = [pageControl currentPage];
}
结合ScrollView后关于UIPageControl的页面设置算法:
// 在ScrollView的委托方法中
int index = fabs(scrollView.contentOffset.x) / scrollView.frame.size.width;
pageControl.currentPage = index;
自定义之一:给UIPageControl控件添加背景
View Code
1 int pagesCount =5;
2 UIPageControl *pageControl = [[UIPageControl alloc] init];
3 pageControl.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height-15); // 设置pageControl的位置
4 pageControl.numberOfPages = pagesCount;
5 pageControl.currentPage = 0;
6
7 [pageControl setBounds:CGRectMake(0,0,16*(pagesCount-1)+16,16)]; //页面控件上的圆点间距基本在16左右。
8 [pageControl.layer setCornerRadius:8]; // 圆角层
9 [pageControl.setBackgroundColor:[UIColor clorWithWhite:0.0 alpha:0.2]];
10 [self.view addSubview:pageControl];
这个效果我找了很久。自己实现过,造了轮子。