UIPageControl和UIScrollView的联合使用
1、首先声明一个UIPageControl
UIPageControl *_pageControl;
2、对UIPageControl进行初始化,并设置总共页数
//初始化_pageControl
_pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(0, 158, (ViewWidth-150)/2, 15)];
[_headView addSubview:_pageControl];
//设置总共页数
_pageControl.numberOfPages=_ADArray.count;
3、UIscrollView拖拽完之后对当前页码赋值
#pragma mark - scrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{ NSLog(@"UIsrollView滚动中");
//计算当前所在页 _pageControl.currentPage=_scrollViewShop.contentOffset.x/ViewWidth;
}
}
4、添加一个计时器
//添加计时器,每3秒钟执行以下nextImage事件
-(void)addTimer
{
//加上[self removeTimer];轮播时间隔时间一致,否则有快有慢
[self removeTimer];
[timerShop fire];
}
5、完成跳至下一个页面的事件
//跳至下一页
-(void)nextImage
{
//以下代码使用UIPageControlShop
int curePage=(int)_pageControl.currentPage;
if (curePage==(totalCount-1)) {
curePage=0;
_pageControl.currentPage=0;
}
else{
curePage++;
}
CGFloat Xshop=curePage*_scrollViewShop.frame.size.width;
_scrollViewShop.contentOffset=CGPointMake(Xshop, 0);
}
注意:1、在使用_scrollViewShop.contentOffset前必须将scrollview的滚动范围_scrollViewShop.contentSize设置好,否则没法滚动,有几张图片就设置几张图片*图片宽度
//设置scrollview的滚动范围
CGFloat contentW=totalCount*imageW;
_scrollViewShop.contentSize=CGSizeMake(contentW, 0);
2、记得在程序在开始要[self addTimer];