UIPageController
UIPageController控件主要用来实现视图分页,通常会和UIScrollView配合使用
创建
UIPageControl *page=[[UIPageControl alloc] init];
设置frame
pagel.frame = CGRectMake(0,self.view.frame.size.height -40,self.view.frame.size.width,20);
常用属性
分页面的数量
page.numberOfPages = self.imageNames.count;
设置翻页标渲染颜色
page.pageIndicatorTintColor = [UIColor blackColor];
设置当前页的渲染颜色
pagel.currentPageIndicatorTintColor =[UIColor redColor];
关闭用户交互
page.userInteractionEnabled = NO;
添加到父视图
[self.view addSubview:page];
UIPageController和UIScrollView配合使用
首先需要遵守<UIScrollViewDelegate>协议,然后在viewDidLoad:方法里面给delegate赋值,
最后实现协议中的scrollViewDidScroll:方法,该方法在每次完成滚动的时候都会被调用,即在该方法里面计算出当前显示第几页并赋值给self.pageControl.currentPage
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView=[[UIScrollView alloc] init];
CGRect frame=self.view.frame;
scrollView.frame=frame;
UIImage *image1=[UIImage imageNamed:@"1.jpg"];
UIImage *image2=[UIImage imageNamed:@"2.jpg"];
UIImage *image3=[UIImage imageNamed:@"3.jpg"];
[email protected][image1,image2,image3];
CGSize size=CGSizeMake(self.array.count*self.view.frame.size.width, self.view.frame.size.height);
//显示内容大小
scrollView.contentSize=size;
for(int i=0;i<self.array.count;i++)
{
UIImageView *imageView=[[UIImageView alloc] initWithImage:self.array[i]];
//图片显示形式
imageView.contentMode=UIViewContentModeScaleAspectFit;
//图片显示范围
imageView.frame=CGRectMake(i*self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
//加到scrollView中
[scrollView addSubview:imageView];
}
//整页翻动
scrollView.pagingEnabled=YES;
UIPageControl *page=[[UIPageControl alloc] init];
self.page=page;
//位置
page.frame = CGRectMake(0,self.view.frame.size.height - 40, self.view.frame.size.width,20);
//页面个数
page.numberOfPages=self.array.count;
//设置颜色
page.currentPageIndicatorTintColor=[UIColor redColor];
page.pageIndicatorTintColor=[UIColor blackColor];
//将自己设置成.delegate
scrollView.delegate=self;
//加到view里
[self.view addSubview:scrollView];
[self.view addSubview:page];
}
//实现方法
//最后实现page的指示标配合scrollView的滚动显示功能,此功能需要通过scrollView的delegate来实现。
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//得到图片移动相对原点的坐标
CGPoint point=scrollView.contentOffset
//移动不能超过左边;
if(point.x<0)
{
point.x=0;
scrollView.contentOffset=point;
}
//移动不能超过右边
if(point.x>(self.array.count-1)*self.view.frame.size.width)
{
point.x=self.view.frame.size.width*(self.array.count-1);
scrollView.contentOffset=point;
}
//根据图片坐标判断页数
NSInteger index=round(point.x/self.view.frame.size.width);
self.page.currentPage=index;
}
声明:参考资料达内笔记