【问题标题】:scrollToItemAtIndexPath after changing cell size更改单元格大小后的 scrollToItemAtIndexPath
【发布时间】:2014-01-25 21:33:19
【问题描述】:

我有一个 UICollectionView,其中包含简单的部分和行,即单元格。使用捏合手势识别器调整单元格的大小。

在单元格调整大小后,我希望旧的第一个可见单元格保持在顶部,以便用户可以控制他放大或缩小到的位置。我存储该单元格的索引路径并调用scrollToItemAtIndexPath,但结果相当随机。有时它会将旧的第一个可见单元格保留在顶部可见行中,但大多数情况下都会失败,并且当使单元格变大时,该单元格位于屏幕外的某个位置,而当使单元格变小时,该单元格位于屏幕中间的某个位置。

我怀疑子视图循环的布局或相关内容不正确,无法正确调用滚动?

-(IBAction)pinch:(UIPinchGestureRecognizer *)pinchRecognizer
{
    CVflowlayout *scaleLayout = (CVflowlayout *)[self.MainCollection collectionViewLayout];

    if (pinchRecognizer.state == UIGestureRecognizerStateBegan) {
        pinchRecognizer.scale = [scaleLayout itemSize] / 50;
    }

    else if (pinchRecognizer.state == UIGestureRecognizerStateChanged || pinchRecognizer.state == UIGestureRecognizerStateEnded)
    {
        cvCellScale = pinchRecognizer.scale;
        NSInteger newCellSize = (int)(50 * cvCellScale);

        // get sorted index paths and first visible cell path
        NSArray *sortedIndexPaths = [MainCollection.indexPathsForVisibleItems sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            NSIndexPath *path1 = (NSIndexPath *)obj1;
            NSIndexPath *path2 = (NSIndexPath *)obj2;
            return [path1 compare:path2];
        }];
        NSIndexPath *sortedFirst = [sortedIndexPaths objectAtIndex:0];
        visibleFirst = [NSIndexPath indexPathForRow:sortedFirst.row inSection:sortedFirst.section];

        scaleLayout.itemSize = CGSizeMake(newCellSize, newCellSize);

        /// ---- What is wrong here ? -------
        [MainCollection setNeedsLayout];
        [MainCollection layoutIfNeeded];
        [MainCollection scrollToItemAtIndexPath:visibleFirst atScrollPosition:UICollectionViewScrollPositionTop animated:NO];

    }

    return;
}

【问题讨论】:

    标签: ios scroll uicollectionview


    【解决方案1】:

    我无法准确解释为什么这不起作用,但我会尝试使用

    [MainCollection setContentOffset:...];
    

    并自己找出内容偏移量。它将是您的单元格高度乘以顶部可见单元格上方的单元格数加上您已有的任何单元格间距。

    【讨论】:

    • 编辑:您可能正在做某事。它可能更简单, [MainCollection layoutAttributesForItemAtIndexPath:indexPath].frame.origin.y;给出 y 坐标,并在此阶段正确更新
    猜你喜欢
    • 2020-09-11
    • 2021-11-20
    • 2021-09-30
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多