iOS开发中的CALayer精析

一、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,另一个坐标并不会变化。

 

原文地址:https://www.cnblogs.com/xiejw/p/5281955.html