前言:我是赵大财,10秒学会系列,绝不废话连篇! 力求10秒,让你了解 会用该 知识点
效果图:
直接上代码
UIView *viewA = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
viewA.backgroundColor = [UIColor redColor];
UIButton *btnA = [UIButton buttonWithType:UIButtonTypeCustom];
btnA.frame = CGRectMake(30, 30, 20, 20);
btnA.backgroundColor = [UIColor yellowColor];
UIView *viewB = [[UIView alloc]initWithFrame:CGRectMake(10, 200, 100, 100)];
viewB.backgroundColor = [UIColor blueColor];
//====上下2种方式 等价====
//CGRect rcA = [viewA convertRect:btnA.frame fromView:viewA];
//CGRect rcB = [self.view convertRect:btnA.frame fromView:viewA];
CGRect rcA = [viewA convertRect:btnA.frame toView:viewA];
CGRect rcB = [viewA convertRect:btnA.frame toView:self.view];
UIButton *btnB = [UIButton buttonWithType:UIButtonTypeCustom];
btnB.frame = rcA;
btnB.backgroundColor = [UIColor yellowColor];
UIButton *btnC = [UIButton buttonWithType:UIButtonTypeCustom];
btnC.frame = rcB;
btnC.backgroundColor = [UIColor yellowColor];
[self.view addSubview:viewA];
[viewA addSubview:btnA];
[self.view addSubview:viewB];
[viewB addSubview:btnB];
[viewB addSubview:btnC];