iOS UICollectionView 在滚动时停在某个item位置上
方法一:实现UIScrollView的代理,然后实现下面这个方法
#pragma mark - UIScrollViewDelegate
//预计出大概位置,经过精确定位获得准备位置
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
CGPoint targetOffset = [self nearestTargetOffsetForOffset:*targetContentOffset];
targetContentOffset->x = targetOffset.x;
targetContentOffset->y = targetOffset.y;
}
//计算落在哪个item上
- (CGPoint)nearestTargetOffsetForOffset:(CGPoint)offset
{
CGFloat pageSize = itemSpacing + itemWidth;
//四舍五入
NSInteger page = roundf(offset.x / pageSize);
CGFloat targetX = pageSize * page;
return CGPointMake(targetX, offset.y);
}
方法二:重写UICollectionViewFlowLayout
参考文献:http://tech.glowing.com/cn/practice-in-uiscrollview/
---------------------
作者:C_calary
来源:CSDN
原文:https://blog.csdn.net/C_calary/article/details/78891724
版权声明:本文为博主原创文章,转载请附上博文链接!
UICollectionView横向移动时,且UICollectionView.page = yes只显示一个Cell 移动时姑且计算划至第几个cell
#pragma mark - collectionView
- (UICollectionView *)collectionView
{
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, screenHeight - kTopHeight - Height_BottomView) collectionViewLayout:layout];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.pagingEnabled = YES;
// _collectionView.scrollEnabled = !GetAnswernextque; // 如果只能滑动至下一题取消滚动效果
[_collectionView registerClass:[AnswerCollectionViewCell class] forCellWithReuseIdentifier:@"AnswerCollectionViewCell"];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.showsVerticalScrollIndicator = NO;
_collectionView.showsHorizontalScrollIndicator = NO;
[self.view addSubview:_collectionView];
}
return _collectionView;
}
#pragma mark - UICollectionViewDelegate
//预计出大概位置,经过精确定位获得准备位置
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
CGPoint targetOffset = [self nearestTargetOffsetForOffset:*targetContentOffset];
targetContentOffset->x = targetOffset.x;
targetContentOffset->y = targetOffset.y;
// self.currentIndexPath
NSInteger index = targetOffset.x/screenWidth;
DLog(@"---------%ld",index);
self.currentIndexPath = [NSIndexPath indexPathForItem:index inSection:0];
self.lbPageIndex.text = [NSString stringWithFormat:@"%li/%li",self.currentIndexPath.item,self.dataSource.count];
}
//计算落在哪个item上
- (CGPoint)nearestTargetOffsetForOffset:(CGPoint)offset
{
CGFloat pageSize = screenWidth;
//四舍五入
NSInteger page = roundf(offset.x / pageSize);
CGFloat targetX = pageSize * page;
return CGPointMake(targetX, offset.y);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint offsetPoint = scrollView.contentOffset;
self.currentIndexPath = [NSIndexPath indexPathForItem:offsetPoint.x/(screenWidth) inSection:0];
self.lbPageIndex.text = [NSString stringWithFormat:@"%li/%li",self.currentIndexPath.item,self.dataSource.count];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
CGPoint offsetPoint = scrollView.contentOffset;
self.currentIndexPath = [NSIndexPath indexPathForItem:offsetPoint.x/(screenWidth) inSection:0];
self.lbPageIndex.text = [NSString stringWithFormat:@"%li/%li",self.currentIndexPath.item,self.dataSource.count];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
CGPoint offsetPoint = scrollView.contentOffset;
self.currentIndexPath = [NSIndexPath indexPathForItem:offsetPoint.x/(screenWidth) inSection:0];
self.lbPageIndex.text = [NSString stringWithFormat:@"%li/%li",self.currentIndexPath.item,self.dataSource.count];
}
#pragma mark - FlipPageDelegate
- (void)flipToNextPage{
CGPoint offsetPoint = self.collectionView.contentOffset;
NSInteger pageIndex = offsetPoint.x / screenWidth;
if (pageIndex < self.dataSource.count - 1) {
pageIndex++;
NSIndexPath *curIndexPath = [NSIndexPath indexPathForItem:pageIndex inSection:0];
self.currentIndexPath = curIndexPath;
self.lbPageIndex.text = [NSString stringWithFormat:@"%li/%li",pageIndex,self.dataSource.count];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:pageIndex inSection:0];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}else{
[ToolUtils showSuccess:@"已经是最后一页"];
}
}
iOS UICollectionView 在滚动时停在某个item位置上的更多相关文章
- [iOS] UICollectionView初始化滚动到中间的bug
转载请保留地址wossoneri.com 问题 首先看一下我之前写的demo:link demo是封装了一个控件,直接在MainViewController的viewWillAppear里初始化,并且 ...
- iOS用contenteditable滚动时,光标不会刷新定位的处理方法
分析 iOS的 wkwebview 在滚动时会暂停许多动画,作为优化 解决思路 监听滚动事件,利用文档重绘即可刷新动画 ps:因为滚动有惯性,touchmove事件只能监听到手指松开的那一刻,所以只能 ...
- iOS 与 惯性滚动
注:以下所有例子均 只 在 iOS 的微信中测试过,但对于饿了么APP的内置浏览器同样适用(两者使用相同内核) 引题 工作中常常有需要显示大量信息的情况,列表超出一屏就涉及到滚动的问题.例如 - va ...
- iOS UICollectionview的详细介绍
转载自:http://jinqianchina.github.io/2015/08/16/UICollectionview%E7%9A%84%E4%BD%BF%E7%94%A8%E8%AF%A6%E8 ...
- iOS:UICollectionView流式布局及其在该布局上的扩展的线式布局
UICollectionViewFlowLayout是苹果公司做好的一种单元格布局方式,它约束item的排列规则是:从左到右依次排列,如果右边不够放下,就换一行重复上面的方式排放,,,,, 常用的 ...
- iOS:UICollectionView纯自定义的布局:瀑布流布局
创建瀑布流有三种方式: 第一种方式:在一个ScrollView里面放入三个单元格高度一样的tableView,禁止tableView滚动,只需让tableView随着ScrollView滚动即可. ...
- 使用 jQuery Ajax 在页面滚动时从服务器加载数据
简介 文本将演示怎么在滚动滚动条时从服务器端下载数据.用AJAX技术从服务器端加载数据有助于改善任何web应用的性能表现,因为在打开页面时,只有一屏的数据从服务器端加载了,需要更多的数据时,可以随着用 ...
- (原创)RecyclerView结合xUtils2.6实现滚动时不加载item,xUtils2.6的源码分析与改造
我们知道xUtils中的bitmapUtils与listview相配合可以实现滚动时暂停加载 只需要一句话: listview.addOnScrollListener(new PauseOnScrol ...
- iOS 导出 ipa 包时 四个选项的意义
iOS 导出 ipa 包时 四个选项的意义 如图 在 iOS 到处 ipa包的时候 会有四个选项 1.Save for iOS App Store Deployment 保存到本地 准备上传App ...
随机推荐
- Android内嵌VLC实现播放网络视频,网络音频
1.在对应模块的build.gradle文件中,添加依赖 //VlC implementation "de.mrmaffen:vlc-android-sdk:2.0.6" 2.布局 ...
- ELF文件的加载过程(load_elf_binary函数详解)--Linux进程的管理与调度(十三)
加载和动态链接 从编译/链接和运行的角度看,应用程序和库程序的连接有两种方式. 一种是固定的.静态的连接,就是把需要用到的库函数的目标代码(二进制)代码从程序库中抽取出来,链接进应用软件的目标映像中: ...
- 【算法】LeetCode算法题-Reverse Integer
这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...
- Python爬虫-01:爬虫的概念及分类
目录 # 1. 为什么要爬虫? 2. 什么是爬虫? 3. 爬虫如何抓取网页数据? # 4. Python爬虫的优势? 5. 学习路线 6. 爬虫的分类 6.1 通用爬虫: 6.2 聚焦爬虫: # 1. ...
- 04.Python网络爬虫之requests模块(1)
引入 Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用. 警告:非专业使用其他 HTTP 库会导致危险的副作用,包括:安全缺陷症.冗余代码症.重新发明轮子症.啃文档 ...
- python 约束与异常处理
一.类的约束 1.约束就是对类的约束.其实就是父类对子类进行约束,子类必须要写xxx方法. 2.抽象:就是当我们没法对一个功能或者一个属性进行精确的表述,一般都采用抽象的方式给出. (1)抽象类的书写 ...
- 【Linux基础】awk命令
1.awk命令说明 (1)awk是行处理器: 相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢出或是处理缓慢的问题,通常用来格式化文本信息 (2)awk处理过程: 依次对每一行进行处理,然后输出 ...
- Understanding Docker
What is Docker? Docker 是一个开源的平台,设计目标是可以方便开发,方便部署和方便执行应用.使用docker可以快速分发开发好的应用.借助于Docker,你可以将开发平台 和应用分 ...
- 【FJWC 2019】 森林
[FJWC 2019] 森林 样例输入 0 5 1 0 0 2 样例输出 1 2 3 3 我们发现,答案就是直径加上直径上某个点出发,不经过其他直径上的点的最长链.这里的直径可以是任意一条直径. 首先 ...
- ContentTypes 的应用
ContentTypes django 中的一个应用程序(app),可以跟踪Django项目中安装的所有模型(Model),提供用于处理模型的高级通用接口. Contenttypes应用的核心是Con ...