简体   繁体   English

获取滚动视图中嵌入的单元格的中心点

[英]Get the Center Point of a Cell Embedded in Scroll View

I have a UICollectionView embedded in a scroll view: 我在滚动视图中嵌入了一个UICollectionView: 看图

Each white square is a collection view cell. 每个白色方块都是一个集合视图单元格。 The user can scroll horizontally to reveal additional cells. 用户可以水平滚动以显示其他单元格。

When the user clicks on a cell, I have created an animation which causes that cell to expand from its own center outward as a transition is made to a new view controller. 当用户点击一个单元格时,我创建了一个动画,当进行到新视图控制器的转换时,该动画会使该单元格从其自身中心向外扩展。

Here is the code: 这是代码:

//cell is selected:

//grab snapshot of cell
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
UIImage *cellImage = [self imageOfCollectionCell:cell]; 

//hold the snapshot in this dict    
NSMutableDictionary *cellImageDict = [[NSMutableDictionary alloc]init];
[cellImageDict setObject:cellImage forKey:SELECTED_INBOX_EMAIL_IMAGE];
[ViewControllerSnapShotDictionary sharedInstance].vcdict = nil;
[ViewControllerSnapShotDictionary sharedInstance].vcdict = cellImageDict; 

//get the center of the cell (HERE IS WHERE THE PROBLEM IS)
CGPoint cellCenter = CGPointMake(cell.center.x, cell.center.y);

//do the animation using the snapshot and cell center
[self startPinchingOutCellFromCenterPoint:cellCenter forTransitionTo:emailController withSnapShotImage:SELECTED_INBOX_EMAIL_IMAGE]; 

The code works fine, EXCEPT if the collection view has been scrolled. 代码工作正常,如果集合视图已滚动,则除外。 The animation requires that I know where the center of the cell is at the moment it is on screen, being touched and relative to the coordinates of the view I am looking at. 动画要求我知道单元格的中心在屏幕上的位置,被触摸以及相对于我正在查看的视图坐标的位置。

For example , if the collection view has not been scrolled, and I select the center cell of the above image, the center might return as: 例如 ,如果尚未滚动集合视图,并且我选择上面图像的中心单元格,则中心可能会返回为:

cell.center.x = 490
cell.center.y = 374

However, if I do a scroll to the right, and then select the new center cell, I might get something like: 但是,如果我向右滚动,然后选择新的中心单元格,我可能会得到类似的结果:

cell.center.x = 1770
cell.center.y = 374

My question is, is there either a better way to accomplish what I am trying to do, OR is there a way to get a handle on the center of the cell as it lies in its current position in self.view? 我的问题是,是否有更好的方法来完成我想要做的事情,或者有没有办法处理单元格的中心,因为它位于self.view中的当前位置?

I think this is because the center coordinates are in the collectionView's coordinate system (which scrolls). 我认为这是因为中心坐标位于collectionView的坐标系(滚动)中。 You need to convert it to a coordinate system that doesn't scroll. 您需要将其转换为不滚动的坐标系。

Try something like this: 尝试这样的事情:

CGPoint realCenter = [collectionView convertPoint:cell.center 
                                     toView:collectionView.superview];

What it does, is basically converts the center from the collectionView's coordinate system to it's parent which should be fixed (not scrolling). 它的作用基本上是将中心从collectionView的坐标系转换为它的父级,它应该是固定的(不是滚动)。

You can even obtain the coordinate on the screen(window) by passing nil as parameter: 您甚至可以通过传递nil作为参数来获取屏幕(窗口)上的坐标:

CGPoint realCenter = [collectionView convertPoint:cell.center 
                                     toView:nil];

It's up to you to decide to which coordinate you want to convert. 由您决定要转换的坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM