frame與bounds是我們UI開(kāi)發(fā)中最常用到的喊崖,只知道frame使用父類(lèi)坐標(biāo)系bounds使用本身坐標(biāo)系,理解的并不夠深入豪直,本文通過(guò)幾個(gè)小栗子來(lái)總結(jié)一下frame與bounds的關(guān)系劣摇,以及修改之后有哪些變化。
- (void)viewDidLoad {
[super viewDidLoad];
UIView * superView = [[UIView alloc] initWithFrame:CGRectMake(30, 70, 350, 350)];
superView.backgroundColor = [UIColor lightGrayColor];
// superView.bounds = CGRectMake(0, 0, 350, 350);
NSLog(@"center:%@",NSStringFromCGPoint(superView.center));
[self.view addSubview:[self showLableForView:superView]];
[self.view addSubview:superView];
UIView * view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 70, 100, 100)];
view1.backgroundColor = [UIColor redColor];
[superView addSubview:[self showLableForView:view1]];
[superView addSubview:view1];
UIView * view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 230, 100, 100)];
view2.backgroundColor = [UIColor yellowColor];
[superView addSubview:[self showLableForView:view2]];
[superView addSubview:view2];
}
2018-03-19 00:09:39.813150+0800 Frame&Bounds[20817:2256742] center:{205, 245}
1弓乙、修改superView的bounds末融,只改origin不改size
superView.bounds = CGRectMake(35, 10, 350, 350);
2018-03-19 18:18:28.535299+0800 Frame&Bounds[78184:2186954] center:{205, 245}
結(jié)果:superView的bounds變化暇韧,子類(lèi)frame與bounds無(wú)變化勾习。
結(jié)論:bounds的origin標(biāo)注本地坐標(biāo)系統(tǒng)的原點(diǎn)。
superview的center無(wú)變化
2懈玻、修改superView的bounds巧婶,只改size不改origin
superView.bounds = CGRectMake(0, 0, 200, 200);
2018-03-19 18:18:56.586907+0800 Frame&Bounds[78220:2188217] center:{205, 245}
結(jié)果:superView的frame與bounds變化涂乌,其center不變superView以center為中心縮放艺栈,子類(lèi)frame與bounds無(wú)變化。
結(jié)論:修改bounds的size影響frame的size湾盒。superview的center未變湿右。
3、修改superView的frame
superView.frame = CGRectMake(0, 100, 200, 350);
2018-03-19 00:12:44.073041+0800 Frame&Bounds[21206:2268036] center:{100, 275}
結(jié)果:superView的frame與bounds變化罚勾,子類(lèi)frame與bounds無(wú)變化毅人。
結(jié)論:修改frame會(huì)影響bounds的size但是不會(huì)影響origin。
superview的center改變尖殃!
4堰塌、旋轉(zhuǎn)superView
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view.center = self.view.center;
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
NSLog(@"view.frame = %@",NSStringFromCGRect(view.frame));
NSLog(@"view.bounds = %@\n",NSStringFromCGRect(view.bounds));
view.transform = CGAffineTransformMakeRotation(M_PI_4);
NSLog(@"view.frame = %@",NSStringFromCGRect(view.frame));
NSLog(@"view.bounds = %@\n",NSStringFromCGRect(view.bounds));
}
2018-03-19 17:39:14.114521+0800 Frame&Bounds[76157:2132120] 旋轉(zhuǎn)前view.frame = {{137.5, 283.5}, {100, 100}}
2018-03-19 17:39:14.114631+0800 Frame&Bounds[76157:2132120] 旋轉(zhuǎn)前view.bounds = {{0, 0}, {100, 100}}
2018-03-19 17:39:14.114724+0800 Frame&Bounds[76157:2132120] 旋轉(zhuǎn)后view.frame = {{116.78932188134526, 262.78932188134524}, {141.42135623730951, 141.42135623730951}}
2018-03-19 17:39:14.114817+0800 Frame&Bounds[76157:2132120] 旋轉(zhuǎn)后view.bounds = {{0, 0}, {100, 100}}
view的frame變化,bounds無(wú)變化分衫。仔細(xì)觀察旋轉(zhuǎn)后的frame ,origin的位置如圖所示般此。并且frame的size不等于bounds的size蚪战。
結(jié)論:frame是view所占的位置的邊框(外切紅色矩形的矩形),bounds是view的邊界铐懊。
總結(jié)如下:
1邀桑、frame采用父類(lèi)的坐標(biāo)系統(tǒng)。
2科乎、bounds使用本身坐標(biāo)系統(tǒng)壁畸,默認(rèn)以原點(diǎn)為起點(diǎn)坐標(biāo)。
3、修改frame的origin不會(huì)影響bounds捏萍,只表示改變本身位于父類(lèi)坐標(biāo)系統(tǒng)的位置太抓。
4、修改bounds的origin令杈,即修改本地坐標(biāo)系統(tǒng)的原點(diǎn)走敌,會(huì)影響子view的顯示位置。
5逗噩、frame與bounds的size不一定相等(旋轉(zhuǎn)情況)掉丽。
6、修改bounds的size异雁,是以center為中心縮放捶障。
7、修改frame的size纲刀,是以原點(diǎn)為中心縮放项炼。