iOS中无限循环滚动简单处理实现
来源:互联网 发布:杀破狼js网盘下载 编辑:程序博客网 时间:2024/06/10 01:50
说下原理:
1./*初始化/
1
+ (instancetype)loopScrollViewWithFrame:(CGRect)frame;
将背景collectinview视图初始化设置 代理和数据源 、 布局
2.在激活initwithFrame后触发 layoutSubviews
1
2
3
4
5
6
//默认滚动到要显示的第一张图片
if
(self.imageCollectionView.contentOffset.x ==
0
) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:
1
inSection:
0
];
[self scrollToIndexPath:indexPath animated:NO];
self.currentIndex =
1
;
}
界面展示出来的时候默认 显示 真实下标也就是从1开始
设置真实数据源 imageList ,然后展示 的 数据源是loopImageList 这里 呢 多出2个对象,0和末尾,设置时 最后 和 起始,setImageList如下
1
2
3
4
5
6
7
8
- (
void
)setImageList:(NSMutableArray *)imageList {
_imageList = imageList;
self.loopImageList = [NSMutableArray arrayWithArray:imageList];
if
(imageList.count>
0
) {
[self.loopImageList insertObject:[imageList lastObject] atIndex:
0
];
[self.loopImageList addObject:[imageList objectAtIndex:
0
]];
}
}
核心代码和思路
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
- (
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);
}
这里的原因是为什么呢?
这时候在图滚动 执行代理 监听的时候 ,我们的collectionview有设置 pageEnable 分页属性很关键有分页动画。
当偏移量判断 真实的数据显示到了最后一张。也就是8 滚到1的时候 ,设置回滚 ,回到默认位置,且没有动画。
另外一步处理当偏移量 小于 一个极小值 也就是 偏移即将到达 0 的是偶也就是 真实的第一张回滚到最后 一张的时候,设置默认滚动到最后一张。
最重要的一点 这个黑科技 是使用scro 滚动到特定的item所以 在触发的那一时刻,item就设定死了,scrollViewDidScroll:也就不会再滚动,因为现在的偏移量是一个唯一值。
阅读全文
0 0
- iOS中无限循环滚动简单处理实现
- iOS 无限循环滚动视图
- 简单粗暴的实现背景图无限循环滚动
- iOS开发之自定义视图上下无限循环滚动实现
- ios用collectionView实现无限滚动的简单日历
- IOS无限自动循环滚动banner(源码)
- iOS UIScrollView滚动视图/无限循环滚动/自动滚动
- iOS UIScrollView滚动视图/无限循环滚动/自动滚动
- Android ListView实现无限循环滚动
- viewpager实现无限循环滚动幻灯片
- 【Android】ViewPager实现无限循环滚动
- android viewpager实现无限循环自动滚动
- Android实现ViewPager无限循环滚动回绕
- IOS中实现类似淘宝广告栏图片无限循环
- ios 滚动视图,滚动广告,无限循环轮播
- iOS UIScrollView无限循环滚动和手动滚动
- iOS无限滚动,实现ios原生日历
- javascript无限循环滚动
- Django源码分析1:创建项目和应用分析
- TabLayout +ViewPager + Fragment : RecyclerView展示网络数据 + webView 显示网页
- 利用apache的ab工具对apache,nginx或者IIS服务器进行压力测试
- 数据结构实验之查找七:线性之哈希表
- 指针的学习
- iOS中无限循环滚动简单处理实现
- C# WebForm <%%>用法
- C# WPF下自定义ComboBox背景、边线、颜色等代码实现
- 吴老师亲自赤膊上阵:MySQL自动化扩容实践~
- 如何解决ajax跨域问题
- 鱼羊儿揭秘《一套手机直播系统源码多少钱》
- 分析java.util.ArrayList的内存分配
- 购物车加减
- M2Crypto安装失败解决