方法一:实现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位置上的更多相关文章

  1. [iOS] UICollectionView初始化滚动到中间的bug

    转载请保留地址wossoneri.com 问题 首先看一下我之前写的demo:link demo是封装了一个控件,直接在MainViewController的viewWillAppear里初始化,并且 ...

  2. iOS用contenteditable滚动时,光标不会刷新定位的处理方法

    分析 iOS的 wkwebview 在滚动时会暂停许多动画,作为优化 解决思路 监听滚动事件,利用文档重绘即可刷新动画 ps:因为滚动有惯性,touchmove事件只能监听到手指松开的那一刻,所以只能 ...

  3. iOS 与 惯性滚动

    注:以下所有例子均 只 在 iOS 的微信中测试过,但对于饿了么APP的内置浏览器同样适用(两者使用相同内核) 引题 工作中常常有需要显示大量信息的情况,列表超出一屏就涉及到滚动的问题.例如 - va ...

  4. iOS UICollectionview的详细介绍

    转载自:http://jinqianchina.github.io/2015/08/16/UICollectionview%E7%9A%84%E4%BD%BF%E7%94%A8%E8%AF%A6%E8 ...

  5. iOS:UICollectionView流式布局及其在该布局上的扩展的线式布局

    UICollectionViewFlowLayout是苹果公司做好的一种单元格布局方式,它约束item的排列规则是:从左到右依次排列,如果右边不够放下,就换一行重复上面的方式排放,,,,,   常用的 ...

  6. iOS:UICollectionView纯自定义的布局:瀑布流布局

    创建瀑布流有三种方式:   第一种方式:在一个ScrollView里面放入三个单元格高度一样的tableView,禁止tableView滚动,只需让tableView随着ScrollView滚动即可. ...

  7. 使用 jQuery Ajax 在页面滚动时从服务器加载数据

    简介 文本将演示怎么在滚动滚动条时从服务器端下载数据.用AJAX技术从服务器端加载数据有助于改善任何web应用的性能表现,因为在打开页面时,只有一屏的数据从服务器端加载了,需要更多的数据时,可以随着用 ...

  8. (原创)RecyclerView结合xUtils2.6实现滚动时不加载item,xUtils2.6的源码分析与改造

    我们知道xUtils中的bitmapUtils与listview相配合可以实现滚动时暂停加载 只需要一句话: listview.addOnScrollListener(new PauseOnScrol ...

  9. iOS 导出 ipa 包时 四个选项的意义

    iOS 导出 ipa 包时 四个选项的意义 如图  在 iOS 到处 ipa包的时候 会有四个选项 1.Save for iOS App Store Deployment 保存到本地 准备上传App ...

随机推荐

  1. CentOS 6.5 安装mysql 过程记录

    下载的时候一定选对应的版本, el6 还是el7 或者其他版本,不然会出现意向不到的惊喜 比如:我刚开始的时候下载的 el7 版本的 mysql , 然后安装的时候 就会出现: libc.so.(GL ...

  2. AndroidNDK开发中使用CMake编译JNI

    虽然一直在做NDK的开发工作,但是由于项目比较久远,都是使用Makefile进行底层编译,对于目前AndroidStudio官方提供的CMake编译方式并不是很了解,现在学习下这种已经不算新潮的新方式 ...

  3. Windows重启显卡驱动热键说明

    Windows 有一个秘密的快捷键,可以重启显卡驱动程序.如果你的电脑经常“冻屏”,可以在重启电脑之前试试这个快捷键,它可以修复冻屏,否则就只能强制重启电脑了. 这个组合快捷键将重启 Win10 和 ...

  4. jQuery -- 光阴似箭(二):jQuery效果的使用

    jQuery -- 知识点回顾篇(二):jQuery效果的使用 1. jQuery 效果 -- 隐藏和显示:  使用 hide() 和 show() 方法来隐藏和显示 HTML 元素. <!DO ...

  5. [福大软工] Z班 团队第一次作业—团队展示成绩公布

    [福大软工] Z班 团队第一次作业-团队展示成绩公布 作业地址 http://www.cnblogs.com/easteast/p/7511264.html 作业要求 队员姓名与学号(标记组长),其中 ...

  6. ABAP 7.50 新特性 – Open SQL中的宿主表达式和其它表达式

    在长期的停滞后,Open SQL的发展终于从沉睡中醒来.从ABAP 7.40开始,SAP推进了某些关键的改变,以尽可能地包含SQL92中的特性,并提供与ABAP CDS中的DDL里面的SELECT一样 ...

  7. 寒假训练——搜索 K - Cycle

    A tournament is a directed graph without self-loops in which every pair of vertexes is connected by ...

  8. 有时间了解一下Spark SQL parser的解析器架构

    1:了解大体架构 2:了解流程以及各个类的职责 3:尝试编写一个

  9. 【转】APK反编译

    学习和开发Android应用有一段时间了,今天写一篇博客总结一下Android的apk文件反编译.我们知道,Android应用开发完成之后,我们最终都会将应用打包成一个apk文件,然后让用户通过手机或 ...

  10. 007_Python中的__init__,__call__,__new__

    __init__函数 当一个类实例被创建时, __init__() 方法会自动执行,在类实例创建完毕后执行,类似构建函数.__init__() 可以被当成构建函数,不过不象其它语言中的构建函数,它并不 ...