1 添加图片
2 设置scrollView的滚动范围 scrollView.contentSize = CGSizeMake(contentW, 0)
3 设置分页 scrollView.pagingEnabled = YES
4 监听scrollView的滚动 scrollView.delegate = self
5 添加计时器
[NSTimer scheduledTimerWithTimeInterval: 2 target:self selector:@selector(nextImage) userInfo: nil repeats:YES];
- (void)nextImage {
if (self.pageControl.currentPage == 4) {
self.pageControl.currentPage = 0
} else {
self.pageControl.currentPage++;
}
滚动时计算页码
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat scrollViewW = scrollView.frame.size.width;
CGFloat x = scrollView.contentOffset.x;
int page = (x + scrollViewW / 2) / scrollViewW;
self.pageControl.currentPage = page;
}
开始拖拽的时候关闭定时器
- (void)scrollVIewWillBeginDragging:(UIScrollView *)scrollView {
[self.timer removeTimer];
}
停止拖拽时重新添加计时器
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView {
[self addTimer];
}
- (void)addTimer {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target: self selector:@selector(nextImage) userInfo:nil repeats: YES];
[[NSRunLOOP currentRunLoop] addTimer:self.timer forMode:@"NSRunLoopCommonModes"];
}
网友评论