一個(gè)面試題讓我去深入了解這些東西匠楚,之前真的很懵
面試題如下:寫(xiě)出各代碼位置 aview bView 的frame 和bounds
- (void)viewDidLoad {
[super viewDidLoad];
UIView * aview = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
aview.backgroundColor = [UIColor redColor];
UIView * bView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
bView.backgroundColor = [UIColor yellowColor];
[aview addSubview:bView];
// aview bView frame bounds
aview.transform = CGAffineTransformMakeScale(2, 2);
// aview bView frame bounds
[self.view addSubview:aview];
aview.layer.anchorPoint = CGPointMake(0, 0);
// aview bView frame bounds
}
frame
- 描述當(dāng)前視圖在其父視圖中的位置和大小
bounds
- 描述當(dāng)前視圖在其自身坐標(biāo)系統(tǒng)中的位置和大小
anchorPoint
- 屬性默認(rèn)值為(0.5,0.5)標(biāo)識(shí)為矩形的中心點(diǎn)巍膘,視圖的幾個(gè)操作都針對(duì)于該點(diǎn)進(jìn)行,比如默認(rèn)旋轉(zhuǎn)一個(gè)視圖就是圍繞該點(diǎn)進(jìn)行旋轉(zhuǎn)芋簿,如果將這個(gè)點(diǎn)改為(0,0)則視圖旋轉(zhuǎn)將會(huì)以視圖左上角旋轉(zhuǎn)峡懈。
position
*position 是layer中的anchorPoint點(diǎn) 在superLayer中的位置坐標(biāo),因此可以說(shuō)position點(diǎn)是相對(duì)superlayer的与斤,anchorPoint是相對(duì)layer的肪康。兩者是相對(duì)不同的左邊空間的一個(gè)重合點(diǎn)
研究代碼
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// 在控制器的視圖被加載到內(nèi)存后調(diào)用。
NSLog(@"%s", __FUNCTION__);
UIView * aview = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
// a frame (100, 100, 100, 100) bounds (0, 0, 100, 100)
aview.backgroundColor = [UIColor redColor];
// anchorPoint 為0.5 0.5 transform 改變frame 不改變bounds
aview.transform = CGAffineTransformMakeScale(2, 2);
// aview 變大寬高變?yōu)?00 中心點(diǎn)沒(méi)變對(duì)應(yīng)的x幽告,y 為 50 50 a frame (50, 50, 200, 200) bounds (0, 0, 100, 100)
[self.view addSubview:aview];
UIView * bView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
bView.backgroundColor = [UIColor yellowColor];
// bview frame bounds 始終沒(méi)變 (0, 0, 50, 50)
[aview addSubview:bView];
// 把 anchorPoint 變?yōu)?(0梅鹦,0)的意思是transform 以(0,0)點(diǎn)做transform操作 此時(shí)bounds不變 frame改變
aview.layer.anchorPoint = CGPointMake(0, 0);
// position.x = frame.origin.x + curr.anchorpoint.x * curr.frame.size.width
// position.y = frame.origin.y + curr.anchorpoint.y * curr.frame.size.height
// CGFloat x = aview.layer.position.x - aview.layer.anchorPoint.x *aview.frame.size.width;
// CGFloat y = aview.layer.position.y - aview.layer.anchorPoint.y *aview.frame.size.height;
// aview 變大寬高變?yōu)?00 anchorPoint變?yōu)椋?冗锁,0) a frame (150, 150, 200, 200) bounds (0, 0, 100, 100)
}
參考資料 (我這個(gè)只是記錄下自己用想了解具體內(nèi)容的看下方連接寫(xiě)的很詳細(xì) )
http://www.reibang.com/p/f5787a1e1d74 關(guān)于position計(jì)算方面有部分錯(cuò)誤
http://www.reibang.com/p/0658e766a627 解釋了bounds 變與不變