美文网首页iOS点点滴滴ios developers
iOS 转换坐标convertRect的基本用法

iOS 转换坐标convertRect的基本用法

作者: calary | 来源:发表于2017-12-15 16:40 被阅读38次

前言

有时候在tableView上需要弹出带箭头的提示框,很苦恼的一件事就是位置无法确定,最近找到了一个转化坐标的方法,问题迎刃而解,赶紧学习下

方法

CGRect rect = [self.view convertRect:button.frame fromView:button.superview];
CGRect rect = [button.superview convertRect:button.frame toView:self.view];
  • convertRect:fromView: 是指从button.superview上将当前的button.frame转换为相对于self.view上的frame,获得相对应的rect

  • convertRect:toView: 是指button.superview 将button.frame转换到self.view 上,获得相对应的rect

  • 一般来说,fromView方法中,消息的接收者为即将转到的目标view,toView方法中,消息的接收者为被转换的frame所在的控件的superview

  • 这种转换对于UIScrollView一样适用

  • convertPoint用法类似,转化点使用

  • 图片分析:button的相对于bgView的frame((0, 0, 100, 100)) -> 得到相对于self.view的frame(0,200,100,100)


    屏幕快照 2017-12-15 下午4.17.58.png

参考

UIView 坐标转换,认识convertRect函数

相关文章

网友评论

    本文标题:iOS 转换坐标convertRect的基本用法

    本文链接:https://www.haomeiwen.com/subject/cnsdwxtx.html