-
(void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat width = self.bounds.size.width;
//在loopImageList中,有n+2个对象,因此index取offset.x/width后的整数
NSInteger index = scrollView.contentOffset.x/width;
//这个比值很重要
CGFloat ratio = scrollView.contentOffset.x/width;
//从显示的最后一张往后滚,自动跳转到显示的第一张
if (index == self.loopImageList.count-1) {
self.currentIndex = 1;
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.currentIndex inSection:0];
[self scrollToIndexPath:indexPath animated:NO];
return;
}
//从显示的第一张往前滚,自动跳转到显示的最后一张
//这里判断条件为contentOffset.x和宽的比值,在往前滚快要结束的时候,能达到无缝切换到显示的最后一张的效果
if (ratio <= 0.01) {
self.currentIndex = self.imageList.count;
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.currentIndex inSection:0];
[self scrollToIndexPath:indexPath animated:NO];
return;
}
if (self.currentIndex != index) {
self.currentIndex = index;
}
NSLog(@"currentIndex = %ld",self.currentIndex);
}