博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS UICollectionView 在滚动时停在某个item位置上
阅读量:5377 次
发布时间:2019-06-15

本文共 4522 字,大约阅读时间需要 15 分钟。

方法一:实现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:@"已经是最后一页"];

    }

}

转载于:https://www.cnblogs.com/sundaysgarden/p/10540677.html

你可能感兴趣的文章
第四周作业
查看>>
线段重叠 (思维好题)
查看>>
软考知识点梳理--项目评估
查看>>
把特斯拉送上火星的程序员,马斯克!
查看>>
三测单
查看>>
MyBatis 缓存
查看>>
SQL中left outer join与inner join 混用时,SQL Server自动优化执行计划
查看>>
mac下python实现vmstat
查看>>
jxl.dll操作总结
查看>>
成员函数对象类的const和非const成员函数的重载
查看>>
机器学习实战-----八大分类器识别树叶带源码
查看>>
eclipse git 新的文件没有add index选项
查看>>
java 泛型
查看>>
VC NetShareAdd的用法
查看>>
java web项目中后台控制层对参数进行自定义验证 类 Pattern
查看>>
图论学习一之basic
查看>>
Java的Array和ArrayList
查看>>
记录Ubuntu 16.04 安装Docker CE
查看>>
安东尼奥·维瓦尔第——巴洛克音乐的奇葩
查看>>
pandas的增删改查
查看>>