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

后端 未结 3 709
感动是毒
感动是毒 2021-02-08 10:37

Is it possible to translate a UICollectionViewCell\'s coordinates from being relative to the UICollectionView to being relative to the superview<

3条回答
  •  南旧
    南旧 (楼主)
    2021-02-08 11:01

    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 UICollectionView is a subclass of UIScrollView.

    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 UICollectionView demo apps and it seems to work. Give it a try.

提交回复
热议问题