簡體   English   中英

更改單元格大小后,使用scrollToItemAtIndexPath

[英]scrollToItemAtIndexPath after changing cell size

我有一個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;
}

我無法確切解釋為什么它不起作用,但是我會嘗試使用

[MainCollection setContentOffset:...];

並找出內容抵消自己。 它是您的像元高度乘以頂部可見像元上方的像元數再加上您已有的任何像元間距。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM