使用convertPoint或者convertRect进行坐标转换时,比较重要的是使用convertRect:toView:时,主语一定是view.superView,也就是一个子view的父视图,(反过来convertRect:fromView:也一样)如果直接用子view做主语,在使用转换后的frame做绘制时,会报错,如果引用转换后的frame时,位置是不准的,切记!
例把UITableViewCell中的subview(btn)的frame转换到 controllerA中
- // 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];
- // 此rc为btn在controllerA中的rect
- 或当已知btn时:
- CGRect rc = [btn.superview convertRect:btn.frame toView:self.view];
- 或
- CGRect rc = [self.view convertRect:btn.frame fromView:btn.superview];
- //如果以上方法写成
- CGRect rc = [btn convertRect:btn.frame toView:self.view];
- CGRect rc = [self.view convertRect:btn.frame fromView:btn];
- //就会报错,或者得到的新frame是错误的