UIScrollView实现无限滚动的三种思路。
当然只是我的一些用法,当然还有非常多的实现方式,如果大家有好的实现思路也可以告诉我,相互学习嘛~
UIScrollView无限滚动
第一种方式:
原理:利用结束的位置来重新设置ScrollView ContentOffset的值,让人产生视觉上的无限循环,优点:代码易懂,缺点,会创建多余的内存。
第二种方式:
原理:利用中间的两个变量来当前的View及缓冲的View,只创建两个View,将当前的View放在中间。判断滑动的位置,优先去缓冲的View找,然后结束滑动在更新当前View的image
第三种方式(这里就不讲解了):
原理:利用CollectionView来实现,代码也非常简单。一次性给数据源传入 images.count * 1000,然后默认加载的时候,跳转到 (images.count * 1000)/2.0 + ((images.count * 1000)/2.0) % images.count的位置就实现了
github代码地址:https://github.com/MakeZL/UIScrollViewLoopDemo点击打开链接
好了。我们先看第一种实现比较简单的
<span style="font-size:14px;">/**
*
第一种方式:
利用结束的位置来重新设置ScrollView ContentOffset的值,让人产生视觉上的无限循环
优点:代码易懂
缺点,会创建多余的内存。
*/
- (void)realizeScrollLoop1{
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.pagingEnabled = YES;
scrollView.frame = self.view.bounds ;
scrollView.delegate = self;
[self.view addSubview:scrollView];
self.scrollView = scrollView;
[scrollView setContentSize:CGSizeMake(([self.slideImages count] + 2) * scrollView.frame.size.width, 0)];
CGSize scrollViewSize = scrollView.frame.size;
// 遍历创建子控件
[self.slideImages enumerateObjectsUsingBlock:^(NSString *imageName, NSUInteger idx, BOOL *stop) {
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:imageName];
imageView.frame = CGRectMake((idx+1) * scrollViewSize.width, 0, scrollViewSize.width, scrollViewSize.height);
[scrollView addSubview:imageView];
}];
// 将最后一张图片弄到第一张的位置
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:
self.slideImages[[self.slideImages count] - 1]];
imageView.frame = CGRectMake(0, 0, scrollViewSize.width, scrollViewSize.height);
[scrollView addSubview:imageView];
// 将第一张图片放到最后位置,造成视觉上的循环
UIImageView *lastImageView = [[UIImageView alloc] init];
lastImageView.image = [UIImage imageNamed:
self.slideImages[0]];
lastImageView.frame = CGRectMake(scrollViewSize.width * ([self.slideImages count] + 1), 0, scrollViewSize.width, scrollViewSize.height);
[scrollView addSubview:lastImageView];
[scrollView setContentOffset:CGPointMake(scrollViewSize.width, 0)];
}</span>
<span style="font-size:14px;">- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSInteger page = scrollView.contentOffset.x / scrollView.frame.size.width;
// 如果当前页是第0页就跳转到数组中最后一个地方进行跳转
if (page == 0) {
[scrollView setContentOffset:CGPointMake(scrollView.frame.size.width * ([[self slideImages] count]), 0)];
}else if (page == [[self slideImages] count] + 1){
// 如果是第最后一页就跳转到数组第一个元素的地点
[scrollView setContentOffset:CGPointMake(scrollView.frame.size.width, 0)];
}
}</span>
第二种方式,带缓存:
<span style="font-size:14px;">/**
*
第二种方式:
利用中间的两个变量来当前的View及缓冲的View,最多创建三个View,将当前的View放在中间。判断滑动的位置,优先去缓冲的View找
优点:对内存消耗少,缺点:代码相比要复杂一丝丝
*/
- (void) realizeScrollLoop2{
status = ScrollViewLoopStatusResuing;
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.pagingEnabled = YES;
scrollView.frame = self.view.bounds ;
scrollView.delegate = self;
[self.view addSubview:scrollView];
self.scrollView = scrollView;
CGSize scrollViewSize = scrollView.frame.size;
scrollView.contentSize = CGSizeMake(3 * scrollViewSize.width, 0);
scrollView.contentOffset = CGPointMake(scrollViewSize.width, 0);
UIImageView *currentView = [[UIImageView alloc] init];
currentView.tag = 0;
currentView.frame = CGRectMake(scrollViewSize.width, 0, scrollViewSize.width, scrollViewSize.height);
currentView.image = [UIImage imageNamed:@"00.jpg"];
[scrollView addSubview:currentView];
self.currentView = currentView;
[self resuingView];
self.index = 0;
}</span><span style="font-size:14px;">- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (status == ScrollViewLoopStatusResuing) {
if (scrollView.contentOffset.x > _currentView.frame.origin.x) {
NSInteger val = self.index + 1;
if (self.index >= [self.slideImages count] - 1) {
val = 0;
}
// 取缓冲区的View
self.resuingView.image = [UIImage imageNamed:[NSString stringWithFormat:@"0%zd.jpg",val]];
self.resuingView.x = CGRectGetMinX(_currentView.frame) + _currentView.width;
self.isLastScrollDirection = YES;
}else{
NSInteger val = self.index - 1;
if (val < 0) {
val = [self.slideImages count]-1;
}
self.resuingView.image = [UIImage imageNamed:[NSString stringWithFormat:@"0%zd.jpg",val]];
self.resuingView.x = 0;
self.isLastScrollDirection = NO;
}
}
}</span>
<span style="font-size:14px;">- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
// 是否是往右边滑动
if (self.isLastScrollDirection) {
self.index++;
}else{
self.index--;
}
// 补全
if (self.index < 0) {
self.index = [self.slideImages count]-1;
} else if(self.index > [self.slideImages count]-1){
self.index = 0;
}
_currentView.image = [UIImage imageNamed:[NSString stringWithFormat:@"0%zd.jpg",self.index]];
}</span>原文地址:http://blog.csdn.net/meters_l/article/details/42145565