xcode 如何找到 UICollectionViewCell 相对于 App Window 的坐标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13554203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:10:53  来源:igfitidea点击:

How to find a UICollectionViewCell's coordinates relative to the App Window

objective-ciosxcodeuicollectionviewuicollectionviewcell

提问by Dan Lister

Is it possible to translate a UICollectionViewCell'scoordinates from being relative to the UICollectionViewto being relative to the superview? So far I've been unable to translate a UICollectionViewCell'scoordinates when touched to the superview. Here's what I've tried so far:

是否可以将UICollectionViewCell's坐标从相对于UICollectionView转换为相对于superview?到目前为止,我一直无法UICollectionViewCell's在触摸到superview. 这是我迄今为止尝试过的:

- (void)collectionView:(UICollectionView *)collectionView  
        didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = 
     [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" 
                                               forIndexPath:indexPath];

    CGRect frame = CGRectMake(0.0, 
                              0.0,  
                              cell.frame.size.width, 
                              cell.frame.size.height);
    frame.origin = [cell convertPoint:cell.frame.origin 
                               toView:self.view];
}

I'd like to create a new frame which originates from the cell's position on the view, not in the UICollectionView. Any help would be great thanks.

我想创建一个新框架,该框架源自视图上单元格的位置,而不是UICollectionView. 任何帮助都会非常感谢。

回答by LeStro

This answer is very late, but hopefully it will help someone. Dan, you have the right idea, but it is even easier that what you have. Just replace

这个答案很晚了,但希望它会帮助某人。丹,你的想法是对的,但比你拥有的更容易。只需更换

CGRect frame = CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height);
frame.origin = [cell convertPoint:cell.frame.origin toView:self.view];

with

CGRect frame = [collectionView convertRect:cell.frame toView:self.view];

The problem was that you were calling the convert method on the cell, not the collectionView.

问题是您在单元格上调用 convert 方法,而不是 collectionView。

回答by Jay Slupesky

If I understand you correctly, I think the problem is that you first have to correct for the scroll view's contentOffset, which is easy to do since UICollectionViewis a subclass of UIScrollView.

如果我理解正确,我认为问题在于您首先必须纠正滚动视图的contentOffset,这很容易做到,因为它UICollectionViewUIScrollView.

CGRect frame = CGRectMake(0.0,
                          0.0,
                          cell.frame.size.width,
                          cell.frame.size.height);
CGPoint correctedOffset = 
 CGPointMake(cell.frame.origin.x - collectionView.contentOffset.x,
             cell.frame.origin.y - collectionView.contentOffset.y);
frame.origin = [cell convertPoint:correctedOffset toView:self.view];

I tested this in one of my UICollectionViewdemo apps and it seems to work. Give it a try.

我在我的一个UICollectionView演示应用程序中对此进行了测试,它似乎有效。试一试。

回答by AbbasAngouti

Just in case if anybody looking for this, here is how it can be done in Swift:

以防万一,如果有人在寻找这个,这里是如何在 Swift 中完成的:

let cellFrameInSuperview = collectionView.convertRect(collectionView.cellForItemAtIndexPath(indexPath)!.frame, toView: view)