/** 点在屏幕中的位置:有以下两个方法
- (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;
- (CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view;
*/
- (void)case2 {
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 15, 100, 100)];
CGPoint point1 = CGPointMake(20, 20);
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 50, 50)];
//点point1 在屏幕中的位置
CGPoint point11A = [view1 convertPoint:point1 toView:self.view];
//同上
CGPoint point11B = [self.view convertPoint:point1 fromView:view1];
//view2 的起点 在屏幕中的位置
CGPoint point22 = [view1 convertPoint: view2.frame.origin toView:self.view];
NSLog(@"--> x1A = %f",point11A.x); // 10+20 = 30
NSLog(@"--> y1A = %f \n ",point11A.y); // 15+20 = 35
NSLog(@"--> x1B = %f",point11B.x); // 10+20 = 30
NSLog(@"--> y1B = %f \n ",point11B.y); // 15+20 = 35
NSLog(@"--> x2 = %f",point22.x); // 10+30 = 40
NSLog(@"--> y2 = %f \n ",point22.y); // 15+30 = 45
}
/**
获取控件相对于屏幕的坐标:有以下两个方法
- (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;
- (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;
*/
- (void)case1 {
[self.view addSubview:self.greenView];
[self.view addSubview:self.redView];
[self.redView addSubview:self.cyanView];
[self.cyanView addSubview:self.yellowView];
CGFloat ww1 = 100;
CGFloat ww2 = 200;
CGFloat ww3 = 300;
CGFloat yy = 150;
_greenView.frame = CGRectMake(20, 20, ww1, ww1);
_redView.frame = CGRectMake(10, yy, ww3, ww3);
_cyanView.frame = CGRectMake(0, 0, ww2, ww2);
_yellowView.frame = CGRectMake(0, 0, ww1, ww1);
_cyanView.center = CGPointMake(_redView.width/2, _redView.height/2);
_yellowView.center = CGPointMake(_cyanView.width/2, _cyanView.height/2);
//fromView
//这段代码的意思算出 在红色控件里的 蓝色控件 在控制器view中的位置(其实就是算x和y的值,因为宽高不变)
CGRect newRect1 = [self.view convertRect:self.cyanView.frame
fromView:self.redView];
// yellowView 在self.view 中的相对坐标 (yellowView 来自 cyanView,因为添加在cyanView上)
CGRect newRect2 = [self.view convertRect:self.yellowView.frame
fromView:self.cyanView];
//同上
CGRect newRect21 = [self.cyanView convertRect:self.yellowView.frame
toView:self.view];
// yellowView:在redView 中的相对坐标 (yellowView 来自 cyanView,因为添加在cyanView上)
CGRect newRect22 = [self.redView convertRect:self.yellowView.frame
fromView:self.cyanView];
//toView
//这段代码的意思是在蓝色控件中定义一个宽高各为100的正方形,相对于蓝色控件的坐标为(50, 50),算出这个正方形在绿色控件中的位置
// yellowView:在greenView 中的相对坐标
CGRect newRect3 = [self.cyanView convertRect:self.yellowView.frame
toView:self.greenView];
//这样写也表明了frame和bounds的区别:frame表示的是在父控件中的位置和大小,bounds表示的是以自身为坐标原点的位置和大小。
CGRect newRect4 = [self.view.window convertRect:self.cyanView.bounds
fromView:self.cyanView];
NSLog(@"--> x_1 = %f",newRect1.origin.x); // 10+50 = 60
NSLog(@"--> y_1 = %f \n ",newRect1.origin.y); // 150+50 = 200
NSLog(@"--> x_2 = %f",newRect2.origin.x); // 10+50+50 = 110
NSLog(@"--> y_2 = %f \n ",newRect2.origin.y); // 150+50+50 = 250
NSLog(@"--> x_22 = %f",newRect22.origin.x); // 50+50 = 100
NSLog(@"--> y_22 = %f \n ",newRect22.origin.y); // 50+50 = 100
NSLog(@"--> x_3 = %f",newRect3.origin.x); //
NSLog(@"--> y_3 = %f \n ",newRect3.origin.y); //
NSLog(@"--> x_4 = %f",newRect4.origin.x); //
NSLog(@"--> y_4 = %f \n ",newRect4.origin.y); //
}
- (UIView *)redView {
if (_redView==nil) {
_redView = [[UIView alloc] initWithFrame:CGRectMake(30, 20, SSWIDTH-60, 300)];
_redView.backgroundColor = Red_COLOR;
}
return _redView;
}
- (UIView *)yellowView {
if (_yellowView==nil) {
_yellowView = [[UIView alloc] initWithFrame:CGRectMake(30, 20, SSWIDTH-60, 300)];
_yellowView.backgroundColor = Yellow_COLOR;
}
return _yellowView;
}
- (UIView *)greenView {
if (_greenView==nil) {
_greenView = [[UIView alloc] initWithFrame:CGRectMake(30, 20, SSWIDTH-60, 300)];
_greenView.backgroundColor = [UIColor greenColor];
}
return _greenView;
}
- (UIView *)cyanView {
if (_cyanView==nil) {
_cyanView = [[UIView alloc] initWithFrame:CGRectMake(30, 20, SSWIDTH-60, 300)];
_cyanView.backgroundColor = CyanColor;
}
return _cyanView;
}