iOS概念:frame,bound,center, anchorPoint

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.