如标题所说,使用的是ScrollView和PageControlle两个控件共同达到该效果
首先,使用storyboard将ScrollView和PageControlle拖入,要注意的是,PageControl不能被ScrollView包含在内,二者要分开属于平级关系
然后要在.h文件中添加UIScrollViewDelegate
然后在.m文件中实现下面代码
-(void) initUI{
//init scrollView
self.automaticallyAdjustsScrollViewInsets=NO;
scrollView.backgroundColor=[UIColor groupTableViewBackgroundColor];
scrollView.bounces=YES;
scrollView.pagingEnabled=YES;
scrollView.scrollEnabled=YES;
scrollView.showsHorizontalScrollIndicator=YES;
scrollView.showsVerticalScrollIndicator=YES;
scrollView.pagingEnabled=YES;
scrollView.delegate=self;
NSArray *array=[NSArray arrayWithObjects:[UIColor orangeColor],[UIColor purpleColor], nil];
for (int i=0; i<array.count; i++) {
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(i*scrollView.frame.size.width,0,scrollView.frame.size.width,scrollView.frame.size.height)];
view.backgroundColor=[array objectAtIndex:i];
[scrollView addSubview:view];
}
scrollView.contentSize=CGSizeMake(scrollView.frame.size.width*3, scrollView.frame.size.height);
//init pageControll
pageControll.backgroundColor=[UIColor groupTableViewBackgroundColor];
pageControll.numberOfPages=array.count;
pageControll.currentPage=0;
}
#pragma ScrollView
-(void) scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"完成滚动");
int index=fabs(scrollView.contentOffset.x)/scrollView.frame.size.width;
pageControll.currentPage=index;
}
-(void) scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"将要开始滚动");
}
-(void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"结束滚动");
}
-(void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(@"滚动将要开始减速");
}
-(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(@"滚动将要减速到停止");
}
-(BOOL) scrollViewShouldScrollToTop:(UIScrollView *)scrollView{
return YES;
}
-(void) scrollViewDidScrollToTop:(UIScrollView *)scrollView{
NSLog(@"滚动到顶部");
}
#pragma ScrollView end