一.官方方法
// 将像素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;
二.使用示例
1.场景图
[图片上传失败...(image-7ceef2-1510195738500)]
2.代码
NSLog(@"SelfView:%@",NSStringFromCGRect(self.view.frame));
NSLog(@"Red:%@",NSStringFromCGRect(_redView.frame));
NSLog(@"Green:%@",NSStringFromCGRect(_greenView.frame));
NSLog(@"Yellow:%@",NSStringFromCGRect(_yellowView.frame));
CGPoint greenToSelf1 = [_redView convertPoint:_greenView.frame.origin toView:self.view];
NSLog(@"%@",NSStringFromCGPoint(greenToSelf1));
CGPoint greenToSelf2 = [self.view convertPoint:_greenView.frame.origin fromView:_redView];
NSLog(@"%@",NSStringFromCGPoint(greenToSelf2));
CGRect yellowToRed1 = [_greenView convertRect:_yellowView.frame toView:_redView];
NSLog(@"%@",NSStringFromCGRect(yellowToRed1));
CGRect yellowToRed2 = [_redView convertRect:_yellowView.frame fromView:_greenView];
NSLog(@"%@",NSStringFromCGRect(yellowToRed2));
3.输出结果
2017-11-09 10:32:46.059083+0800 KeyBoardNotification[2924:799686] SelfView:{{0, 0}, {414, 736}}
2017-11-09 10:32:46.059150+0800 KeyBoardNotification[2924:799686] Red:{{100, 20}, {200, 200}}
2017-11-09 10:32:46.059173+0800 KeyBoardNotification[2924:799686] Green:{{50, 20}, {100, 100}}
2017-11-09 10:32:46.059194+0800 KeyBoardNotification[2924:799686] Yellow:{{25, 20}, {50, 50}}
2017-11-09 10:32:46.059223+0800 KeyBoardNotification[2924:799686] {150, 40}
2017-11-09 10:32:46.059244+0800 KeyBoardNotification[2924:799686] {150, 40}
2017-11-09 10:32:46.059268+0800 KeyBoardNotification[2924:799686] {{75, 40}, {50, 50}}
2017-11-09 10:32:46.059289+0800 KeyBoardNotification[2924:799686] {{75, 40}, {50, 50}}