在我们日程开发中,往往会遇到获取子类view在父类view的frame,比如获取一封装view中的某一个子view在controller的view上的frame
- 将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
- 将像素point从view中转换到当前视图中,返回在当前视图中的像素值
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;
- 将rect由rect所在视图转换到目标视图view中,返回在目标视图view中的rect
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
- 将rect从view中转换到当前视图中,返回在当前视图中的rect
- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;
下面来举个例子:
- 把UITableViewCell中的subview(button)的frame转换到 controller.view中
//在controller 中实现:
CGRect btnRect = [cell convertRect:cell.button.frame toView:self.view];
//也可以写成
CGRect btnRect = [self.view convertRect:cell.button.frame fromView:cell];
// 此btnRect为button在controller中的rect
CGRect btnRect = [button.superview convertRect:button.frame toView:self.view];
//也可以写成
CGRect btnRect = [self.view convertRect:button.frame fromView:button.superview];