1.創(chuàng)建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.創(chuàng)建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.滾動時觸發(fā)方法
-(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設(shè)置為tableView頭視圖
_tableView.tableHeaderView = _vieww;