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

        }

    }

  • 相关阅读:
    ES6 Promise 用法转载
    移动端滚动性能优化
    Python之禅
    Day01~15
    Python
    第一章 Java起源
    IMP-00009: 导出文件异常结束 imp
    浏览器访问网页的详细内部过程
    数据库连接池
    连接数据库 六大步骤
  • 原文地址:https://www.cnblogs.com/sundaysgarden/p/10540677.html
Copyright © 2020-2023  润新知