iOS开发中坐标系的转换

一.官方方法

// 将像素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}}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,095评论 25 709
  • 目标 儿子积极上进,身心健康,努力学习,考上她理想的高中。 自己身心健康,不看无聊的网页,不看没有意义的视频,不说...
    7f207b2df6aa阅读 1,198评论 1 2
  • DrawerLayout在高版本(Android4.4以及以上)的v4包中侧边菜单可以根据手势展开与隐藏(draw...
    凯玲之恋阅读 4,210评论 0 1
  • 文人多是悲情的 因为悲情 才会用写的方式去记录 善良的悲情 用这样的方式分享 你的目光 出卖了你心里真实的想法 因...
    那个在看大象洗澡的人阅读 2,806评论 0 0
  • 写在前面的话 三节课并不是新东方、学而思此类的教育界巨头。 在三节课刚刚建立的初期,创始人Luke、布棉利用自己在...
    课橙阅读 12,992评论 0 24

友情链接更多精彩内容