您的位置:首页 > 移动开发 > IOS开发

iOS 转换坐标系

2016-04-15 14:34 387 查看
iOS中视图坐标系的转换主要用到UIView的四个API:

坐标系转换的实质是: 更改坐标系的原点.

- (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;

- (CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view;

- (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;

- (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;

代码:

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong)UIView *redView;
@property(nonatomic,strong)UIView *blueView;
@property(nonatomic,strong)UIView *yellowView;
@property(nonatomic,strong)UIView *orangeView;
@end

@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_redView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 300, 300)];
_redView.backgroundColor = [UIColor redColor];
[self.view addSubview:_redView];

_blueView = [[UIView alloc]initWithFrame:CGRectMake(30, 30, 250, 250)];
_blueView.backgroundColor = [UIColor blueColor];
[_redView addSubview:_blueView];

_yellowView = [[UIView alloc]initWithFrame:CGRectMake(30, 30, 200, 200)];
_yellowView.backgroundColor = [UIColor yellowColor];
[_blueView addSubview:_yellowView];

_orangeView = [[UIView alloc]initWithFrame:CGRectMake(30, 30, 150, 150)];
_orangeView.backgroundColor = [UIColor orangeColor];
[_yellowView addSubview:_orangeView];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

NSLog(@"橘色view在父控件中的frame = %@",NSStringFromCGRect(self.orangeView.frame));
// 两种写法 含义相同 第二种写法 更加清晰
// 如果toView为空 默认为当前主窗口 即UIWindow
CGRect orange_in_blue_frame1 = [self.orangeView.superview convertRect:self.orangeView.frame toView:self.blueView];
CGRect orange_in_blue_frame2 = [self.orangeView convertRect:self.orangeView.bounds toView:self.blueView];
NSLog(@"橘色view在蓝色view中的frame1 = %@",NSStringFromCGRect(orange_in_blue_frame1));
NSLog(@"橘色view在蓝色view中的frame2 = %@",NSStringFromCGRect(orange_in_blue_frame2));

CGRect orange_in_red_frame = [self.orangeView.superview convertRect:self.orangeView.frame toView:self.redView];
NSLog(@"橘色view在红色view中的frame = %@",NSStringFromCGRect(orange_in_red_frame));

CGRect orange_in_VCView_frame = [self.orangeView.superview convertRect:self.orangeView.frame toView:self.view];
NSLog(@"橘色view在控制器view中的frame = %@",NSStringFromCGRect(orange_in_VCView_frame));

CGRect yellow_in_VCView_frame = [self.view convertRect:self.yellowView.frame fromView:self.yellowView.superview];
NSLog(@"黄色view在控制view中的frame = %@",NSStringFromCGRect(yellow_in_VCView_frame));
}
@end


打印结果:

2016-04-15 15:51:33.543 坐标系转换[2744:154111] 橘色view在父控件中的frame = {{30, 30}, {150, 150}}

2016-04-15 15:51:33.544 坐标系转换[2744:154111] 橘色view在蓝色view中的frame1 = {{60, 60}, {150, 150}}

2016-04-15 15:51:33.544 坐标系转换[2744:154111] 橘色view在蓝色view中的frame2 = {{60, 60}, {150, 150}}

2016-04-15 15:51:33.544 坐标系转换[2744:154111] 橘色view在红色view中的frame = {{90, 90}, {150, 150}}

2016-04-15 15:51:33.544 坐标系转换[2744:154111] 橘色view在控制器view中的frame = {{140, 140}, {150, 150}}

2016-04-15 15:51:33.544 坐标系转换[2744:154111] 黄色view在控制view中的frame = {{110, 110}, {200, 200}}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: