iOS坐标转换

转换方法声明 @interface UIView(UIViewGeometry)

// 将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值
- (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;

// 将像素point从view中转换到当前视图中,返回在当前视图中的像素值
- (CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view;

// 将rect由rect所在视图转换到目标视图view中,返回在目标视图view中的rect
- (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;

// 将rect从view中转换到当前视图中,返回在当前视图中的rect
- (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;

案例

  • controllerA 中有一个UITableView, UITableView里有多行UITableVieCell,cell上放有一个button
// 在controllerA中实现:
CGRect rc = [cell convertRect:cell.btn.frame toView:self.view];

或
CGRect rc = [self.view convertRect:cell.btn.frame fromView:cell];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • // 将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值 - (CGP...
    nemoispretty阅读 588评论 0 0
  • //将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值 - (CGPo...
    cornerAnt阅读 433评论 0 3
  • 官方提供了4个方法(UIView的方法): -(CGPoint)convertPoint:(CGPoint)poi...
    芝麻酱的简书阅读 1,509评论 0 1
  • 说明:同一屏幕上的视频,可以进行坐标转换。 UIView方法1 ------------------------...
    IMKel阅读 529评论 0 1
  • 和公交有关的故事 每到一个陌生的城市,我们都愿意体验一下当地的公共交通。 伊朗的地铁有专门的女士车厢,公交车的后半...
    Echo_Quan阅读 320评论 0 0