iOS開發中的CALayer精析

來源:互聯網
上載者:User

標籤:

一、UIView和CALayer

      首先,我們來看繼承關係,UIView->UIResponder->NSObject ,而CALayer直接繼承自NSObject,可見在NSObject的樹形模型中,CALayer比UIView層級要高,但這並不能說明什麼,那麼我們先來看一下CALayer的API文檔中的解釋。

  • The CALayer class manages image-based content and allows you to perform animations on that content. Layers are often used to provide the backing store for views but can also be used without a view to display content. A layer’s main job is to manage the visual content that you provide but the layer itself has visual attributes that can be set, such as a background color, border, and shadow. In addition to managing visual content, the layer also maintains information about the geometry of its content (such as its position, size, and transform) that is used to present that content onscreen. Modifying the properties of the layer is how you initiate animations on the layer’s content or geometry. A layer object encapsulates the duration and pacing of a layer and its animations by adopting the CAMediaTiming protocol, which defines the layer’s timing information. If the layer object was created by a view, the view typically assigns itself as the layer’s delegate automatically, and you should not change that relationship. For layers you create yourself, you can assign a delegate object and use that object to provide the contents of the layer dynamically and perform other tasks. A layer may also have a layout manager object (assigned to the layoutManager property) to manage the layout of subviews separately.

        大致意思是:CALayer類管理基於映像內容,並允許您執行動畫內容。圖層通常用來提供view不具有的屬性和方法,但也可以通過視圖來顯示內容。圖層的主要工作是管理您提供的視覺內容,但圖層本身屬性也可以設定,如背景顏色,邊界,和陰影。除了管理視覺內容,圖層還有幾何資訊(比如它的位置、大小和變換),通過設定這些內容,圖層可以顯示出來。修改圖層的屬性可以用來設定基於圖層本身和位置的動畫。圖層對象包含圖層的期間和基於CAMediaTiming協議(定義了層的計時資訊)動畫效果。

       如果圖層對象基於view,view通常通過代理自動化佈建圖層,沒必要進行修改。圖層建立後,可以指定一個委派物件,並使用該對象提供的屬性便捷的執行其他任務。圖層也有一個布局管理器對象(分配給layoutManager屬性)來分別管理子視圖的布局。

        相信你已經對layer有了初步認識,每一個UIView內部都預設關聯著一個CALayer, UIView有frame、bounds和center三個屬性,CALayer也有類似的屬性,分別為frame、bounds、position、anchorPoint。下面就position、anchorPoint做進一步說明。

 二、position  和anchorPoint

@property CGPoint position @property CGPoint anchorPoint

     舉個例子,把一張A4白紙用圖釘訂在書桌上,如果訂得不是很緊的話,白紙就可以沿順時針或逆時針方向圍繞圖釘旋轉,這時候圖釘就起著支點的作用。我們要解釋的anchorPoint就相當於白紙上的圖釘,它主要的作用就是用來作為變換的支點,旋轉就是一種變換,類似的還有平移、縮放。

     很明顯,白紙的旋轉形態隨圖釘的位置不同而不同,圖釘訂在白紙的正中間與左上方時分別造就了兩種旋轉形態,這是由圖釘(anchorPoint)的位置決定的。如何衡量圖釘(anchorPoint)在白紙中的位置呢?在iOS中,anchorPoint點的值是用一種相對bounds的比例值來確定的,在白紙的左上方、右下角,anchorPoint分為為(0,0), (1, 1),也就是說anchorPoint是在單元座標空間(同時也是左手座標系)中定義的。白紙的中心點、左下角和右上方的anchorPoint為(0.5,0.5), (0,1), (1,0)。     

     再來看看position的官方定義:

  •       The layer’s position in its superlayer’s coordinate space。 

     即position是layer相對superLayer座標空間的位置

          在API對frame的描述中有這麼一句話:

  •      Layers have an implicit frame that is a function of the position, bounds, anchorPoint, and transform properties.
  •      When you specify the frame of a layer, position is set relative to the anchor point. When you specify the position of the layer, bounds is set relative to the anchor point.

      position是layer中的anchorPoint點在superLayer中的位置座標。因此可以說, position點是相對suerLayer的,anchorPoint點是相對layer的,兩者是相對不同的座標空間的一個重合點。

      當設定圖層的frame屬性的時候,position根據錨點(anchorPoint)的值來確定,而當你設定圖層的position屬性的時候,bounds會根據錨點(anchorPoint)來確定。

     當設定圖層的frame屬性的時候,position點的位置(也就是position座標)根據錨點(anchorPoint)的值來確定,而當你設定圖層的position屬性的時候,bounds的位置(也就是frame的orgin座標)會根據錨點(anchorPoint)來確定。

我們可以得到如下結論:

   1、position是CGPoint類型,bounds是CGRect類型,根據錨點(anchorPoint)來確定的只是它們的位置。

   2、和UIView類似,CALayer也有superLayer與sublayer,他們各自有相對的座標空間。

   3、關聯性:position 用來確定在父layer中的位置, 是子layer的anchorPoint在superLayer中的位置座標。 

   4、互異性:單獨修改position與anchorPoint中任何一個屬性都不影響另一個屬性。 position與anchorPoint是處於不同座標空間中的重合點,修改重合點在一個座標空間的位置不影響該重合點在另一個座標空間中的位置。如果單獨修改,改變的只會移動layer,另一個座標並不會變化。

 

iOS開發中的CALayer精析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.