坐标转换

坐标转换_第1张图片
display_image.png
  • (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;
  • (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;

Aview 在 Bview 上的 frame 转换到 Cview 坐标系下 frame (Aview 必需是 Bview 的 subview);

代码:
[Bview convertRect:Aview.frame toView:Cview];
[Aview convertRect:Aview.bounds toView:Cview];
[Cview convertRect:Aview.frame fromView:Bview];
[Cview convertRect:Bview.bounds fromeView:Bview];
- (void)viewDidLoad {
[super viewDidLoad];

NSLog(@"C : YELLOW --- %@", NSStringFromCGRect(self.yellowView.frame));
NSLog(@"B : RED ------ %@", NSStringFromCGRect(self.redView.frame));
NSLog(@"A : BLUE ----- %@", NSStringFromCGRect(self.blueView.frame));

} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGRect rect1 = [self.redView convertRect:self.yellowView.frame toView:self.blueView];
CGRect rect2 = [self.yellowView convertRect:self.yellowView.bounds toView:self.blueView];
CGRect rect3 = [self.blueView convertRect:self.yellowView.frame fromView:self.redView];
CGRect rect4 = [self.blueView convertRect:self.yellowView.bounds fromView:self.yellowView];
NSLog(@"\n\n");
NSLog(@"RESULT1 ------ %@", NSStringFromCGRect(rect1));
NSLog(@"RESULT2 ------ %@", NSStringFromCGRect(rect2));
NSLog(@"RESULT3 ------ %@", NSStringFromCGRect(rect3));
NSLog(@"RESULT4 ------ %@", NSStringFromCGRect(rect4));
}

打印结果:

C : YELLOW --- {{50, 50}, {135, 120}}
B : RED ------ {{40, 40}, {235, 220}}
A : BLUE ----- {{30, 30}, {315, 300}}

RESULT1 ------ {{90, 90}, {135, 120}}
RESULT2 ------ {{90, 90}, {135, 120}}
RESULT3 ------ {{90, 90}, {135, 120}}
RESULT4 ------ {{90, 90}, {135, 120}}

你可能感兴趣的:(坐标转换)