標籤:center gre view ide log strong color 相對 計算
frame: View在它的Super View座標系裡的座標
bound: 用來定義View自身座標系和邊界的Rect,Rect的原點表示View自身座標系的原點座標。舉個例子:
一般情況下bound的值為(0,0,width,heigh),其中0,0表示View自身座標系的原點座標為(0,0)
但是既然類型是Rect,我也可以設定bound的原定為非(0,0)啊,比如bound值為(10,10,width,height). 這樣則表示View自身的座標系中原點的座標為(10,10).這對於View自身其實沒有影響,但對於View的subView,他們的顯示位置可能就要用frame的值減去這個原點的值了。看如下代碼:
class LayerViewController: UIViewController { private var subLayer : CALayer!; override func viewDidLoad() { super.viewDidLoad() let subView = UIView(); subView.backgroundColor = UIColor.blueColor(); subView.bounds = CGRect(x: 50, y: 50, width: 50, height: 50); subView.frame = CGRect(x: 100, y: 100, width: 50, height: 50); let thirdView = UIView(); thirdView.backgroundColor = UIColor.redColor(); thirdView.frame = CGRect(x: 50, y: 50, width: 10, height: 10); subView.addSubview(thirdView); self.view.addSubview(subView); }}
UI呈現效果:
雖然thirdView的frame設為(50,50,50,50),但是因為subView的bound原點為(50,50).因此thirdView顯示在subView的左上方。
anchorPoint:
其實是CALayer中的屬性沒有在View中暴露出來。因此我們如果需要修改需要通過修改View的關聯Layer的anchorPoint屬性來實現。anchorPoint是用來確定在做微調,向內等操作時用來做參考點的點。他的值是一個相對與bound的的值,在bound內,則取值範圍為(0,0)~(1,1)
center:
表示View的anchorPoint在super view的座標系中的位置。
frame與bound都能這隻View的weight和height, 那可以設定不一樣嗎?答案是肯定的。其實frame是一個虛擬屬性,它的值是通過bound和center計算出來的。同時當給frame賦值時,bound和center的值也會被重設。
iOS概念:frame,bound,center, anchorPoint