1. frame
frame表示的是該view在父坐標(biāo)系統(tǒng)中的位置和大薪汗(參照父坐標(biāo)系統(tǒng))拱镐,frame的(frame.origin.x, frame.origin.y)是相對(duì)于父坐標(biāo)系的偏移量
- (CGRect)frame {
return CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
}
2. bounds
bounds表示的是該view在本地坐標(biāo)系統(tǒng)中的位置和大星徽佟(參照本地坐標(biāo)系統(tǒng)拾给,就相當(dāng)于View自己的坐標(biāo)系統(tǒng)请祖,以(0, 0)點(diǎn)為起點(diǎn))
- (CGRect)bounds {
return CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}
- bounds特點(diǎn):可以通過(guò)修改自己坐標(biāo)系的原點(diǎn)位置馒疹,進(jìn)而影響到“子view”的顯示位置谬泌;可以改變的frame滔韵,如果bounds>frame,那么frame也會(huì)跟著變大
注意
在實(shí)際使用中可以通過(guò).bounds
或者setBounds
可以自定義視圖的bounds掌实,當(dāng)然在自定義之后陪蜻,其內(nèi)部的子控件位置也會(huì)發(fā)生相關(guān)變化,具體變化的規(guī)律我做了一下簡(jiǎn)單的總結(jié)贱鼻,下面就通過(guò)一個(gè)偽代碼的式子來(lái)描述一下(獲取子控件位置的y同理)
/** 獲取子控件最終frame.origin.x(相對(duì)于根視圖)
* superView.bounds.origin.x:父view自定義bounds的x
* superView.frame.size.width:父view原本的width
* superView.bounds.size.width:父view自定義bounds的width
* superView.frame.origin.x:父view原本的x
*********************** 華麗的分割線 ***********************
* subView.bounds.origin.x:子view自定義bounds的x
* subView.frame.size.width:子view原本的width
* subView.bounds.size.width:子view自定義bounds的width
* subView.frame.origin.x:子view原本的x
*/
lastX = (-(superView.bounds.origin.x) + (superView.frame.size.width - superView.bounds.size.width) / 2.0 + superView.frame.origin.x) + (- subView.bounds.origin.x + (subView.frame.size.width - subView.bounds.size.width) / 2.0 + subView.frame.origin.x))
3. Demo
有興趣的話大家可以親測(cè)一下
UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 350, 350)];
v1.backgroundColor = [UIColor redColor];
v1.bounds = CGRectMake(20, 20, 250, 250);
[self.view addSubview:v1];
NSLog(@"%lf~~~~%lf~~~~%lf~~~~%lf", v1.frame.origin.x, v1.frame.origin.y, v1.frame.size.width, v1.frame.size.height);
UIView *v2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
v2.backgroundColor = [UIColor orangeColor];
[v1 addSubview:v2];
NSLog(@"%lf~~~~%lf", v2.frame.origin.x, v2.frame.origin.y);