1.创建scrollView
-(void)createScrolllView{
self.vieww = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 200)];
self.sv = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 375, 200)];
_sv.backgroundColor =[UIColor yellowColor];
_sv.contentSize = CGSizeMake(375*3, 200);
_sv.delegate= self;
_sv.pagingEnabled = YES;
[self.vieww addSubview:_sv];
2.创建pageControl
self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 400,100 , 20)];
_pageControl.backgroundColor = [UIColor blackColor];
_pageControl.numberOfPages =3;
_pageControl.center = CGPointMake(375/2, 180);
[_pageControl addTarget:self action:@selector(page:) forControlEvents:(UIControlEventValueChanged)];
_pageControl.tag = 1000;
[self.vieww addSubview:_pageControl];
}
3.使pageControl自动滚动(间隔0.5s)
-(void)page:(UIPageControl *)page{
[UIView animateWithDuration:0.5 animations:^{
self.sv.contentOffset = CGPointMake(375* page.currentPage, 0);
}];
}
4.使图片自动滚动(间隔0.5s)
-(void)autoRoll{
[UIView animateWithDuration:0.5 animations:^{
self.sv.contentOffset = CGPointMake(self.sv.contentOffset.x + 375, 0);
} completion:^(BOOL finished) {
if (self.sv.contentOffset.x/375 == 3) {
self.sv.contentOffset = CGPointZero;
}
}];
}
5.滚动时触发方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//当滚动到最后一页时 跳回第一页
if (self.sv.contentOffset.x/375 == 6) {
self.sv.contentOffset = CGPointMake(0, 0);// = CGPointZero
}
//修改小圆点
UIPageControl *pc = [self.view viewWithTag:1000];
pc.currentPage = self.sv.contentOffset.x/375;
}
6.将views设置为tableView头视图
_tableView.tableHeaderView = _vieww;