我有一个包含简单节和行的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;
}
答案 0 :(得分:0)
我无法准确解释为什么这不起作用,但我会尝试使用
[MainCollection setContentOffset:...];
并自己计算内容偏移量。它将是您的单元格高度乘以顶部可见单元格上方的单元格数量加上您所拥有的任何单元格间距。