UIPageControl+UIScrollView

UIPageControl继承了UIControl基类,默认属于活动控件,它可以与用户进行交互,经常与UIScrollerView结合使用,在实际项目也是经常使用的。当把UIScrollView的pagingEnabled设置为YES之后,,UIScrollView至少每次滚动一页。此时通常结合UIPageControl使用,UIPageControl控件会充当两个功能。 使用UIPageControl显示当前的UIScrollView正在显示第几页。 当用户点击UIPageControl控件时程序控制UIScrollView自动滚动到相应的页面。 为将要显示的图片们进行初始化: coverList=[NSArray arrayWithObjects:@”11.png”,@”22.png”,@”33.png”, nil]; 获取图片的个数以备后用: 创建UIScrollPane对象 scrollView=[[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; 创建背景颜色 scrollView.backgroundColor=[UIColor grayColor]; 设置可以手动挑动图片 scrollView.pagingEnabled=YES; 设置UIScrollPane的contentSize—就是它可滚动区域的大小 scrollView.contentSize=CGSizeMake(CGRectGetWidth(scrollView.frame)*numberPages, CGRectGetHeight(scrollView.frame)); scrollView.showsHorizontalScrollIndicator=NO; scrollView.showsVerticalScrollIndicator=NO; scrollView.scrollsToTop=NO; 设置该控件委托对象 scrollView.delegate=self; [self.view addSubview:scrollView]; 接下来初始化UIPage pageControl=[[UIPageControl alloc]init]; 设置未选中时圆点的颜色 pageControl.pageIndicatorTintColor=[UIColor grayColor]; 设置选中高亮时圆点的颜色 pageControl.currentPageIndicatorTintColor=[UIColor redColor]; 设置默认在页面数 pageControl.currentPage=0; 设置UIPageControl总共包含多少页 pageControl.numberOfPages=numberPages; pageControl.hidesForSinglePage=YES; 监听事件: [pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged]; 初始化时默认只需加载、显示第一页的View [self loadScrollViewWithPage:0]; 加载UIScrollPage的指定页对应的控制器 -(void)loadScrollViewWithPage:(NSUInteger)page { if (page>=coverList.count) { return; } WXPageController *contorller=[viewControllers objectAtIndex:page]; if ((NSNull *)contorller==[NSNull null]) { contorller=[[WXPageController alloc]initWithPageNumber:page]; [viewControllers replaceObjectAtIndex:page withObject:contorller]; } if (contorller.view.superview==nil) { CGRect frame=scrollView.frame; frame.origin.x=CGRectGetWidth(frame)*page; frame.origin.y=0; contorller.view.frame=frame; contorller.image.image = [UIImage imageNamed:[coverList objectAtIndex:page]];[self addChildViewController:contorller]; [scrollView addSubview:contorller.view]; } } 来自UIScrollViewDelegate的方法,当用户滚动了UIScrollView后激发该方法 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { CGFloat pageWith=CGRectGetWidth(scrollView.frame); NSUInteger page=floor((scrollView.contentOffset.x-pageWith/2)/pageWith)+1; pageControl.currentPage=page; [self loadScrollViewWithPage:page-1]; [self loadScrollViewWithPage:page]; [self loadScrollViewWithPage:page+1];

}

事件监听,当用户更改UIPageControl的选中页激发方法 -(void)changePage:(id)sender {

NSInteger page=[sender currentPage];CGRect bounds=scrollView.bounds;bounds.origin.x=CGRectGetWidth(bounds)*page;bounds.origin.y=0;[scrollView scrollRectToVisible:bounds animated:YES];[self loadScrollViewWithPage:page – 1];[self loadScrollViewWithPage:page];[self loadScrollViewWithPage:page + 1];

} 在WXPageController中声明UIImageView变量和声明并实现 -(id)initWithPageNumber:(NSInteger)pageNumber { self=[super initWithNibName:nil bundle:nil]; if (self) { self.image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 90, CGRectGetWidth(self.view.frame), 320)]; self.image.contentMode=UIViewContentModeScaleAspectFit; [self.view addSubview:self.image]; } return self; }方法 最后分享一下,我的模板代码:

但一定要背上几本书,在花海里,草丛旁悠然品味,

UIPageControl+UIScrollView

相关文章:

你感兴趣的文章:

标签云: