Ios 获取内嵌在滚动视图中的单元格的中心点

Ios 获取内嵌在滚动视图中的单元格的中心点,ios,objective-c,uiscrollview,uicollectionview,cgpoint,Ios,Objective C,Uiscrollview,Uicollectionview,Cgpoint,我在滚动视图中嵌入了UICollectionView: 每个白色正方形都是一个集合视图单元。用户可以水平滚动以显示其他单元格 当用户单击一个单元时,我创建了一个动画,当转换到一个新的视图控制器时,该动画会使该单元从它自己的中心向外扩展 代码如下: //cell is selected: //grab snapshot of cell UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath]; U

我在滚动视图中嵌入了UICollectionView:

每个白色正方形都是一个集合视图单元。用户可以水平滚动以显示其他单元格

当用户单击一个单元时,我创建了一个动画,当转换到一个新的视图控制器时,该动画会使该单元从它自己的中心向外扩展

代码如下:

//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]; 
代码工作正常,除非集合视图已滚动。动画要求我知道当单元格在屏幕上时,它的中心在哪里,被触摸,并且相对于我所看到的视图的坐标

例如,如果集合视图尚未滚动,并且我选择了上面图像的中心单元格,则中心可能返回为:

cell.center.x = 490
cell.center.y = 374
但是,如果向右滚动,然后选择新的中心单元格,可能会得到如下结果:

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

我的问题是,有没有更好的方法来完成我想做的事情,或者有没有一种方法可以在单元格的中心获得一个句柄,因为它位于self.view中的当前位置?

我认为这是因为中心坐标在collectionView的坐标系中(滚动)。您需要将其转换为不滚动的坐标系

试着这样做:

CGPoint realCenter = [collectionView convertPoint:cell.center 
                                     toView:collectionView.superview];
它所做的,基本上是将中心从collectionView的坐标系转换为其父坐标系,该坐标系应该是固定的(而不是滚动)

您甚至可以通过将nil作为参数传递来获得屏幕(窗口)上的坐标:

CGPoint realCenter = [collectionView convertPoint:cell.center 
                                     toView:nil];
由您决定要转换到哪个坐标