1023 -- iOS之CALayer类

     这个就真的是属性方法太多了,而且很多不经常用,就不一一翻译了,之前那些翻译也没有时间写使用总结,还有避坑,有时间再搞了。只翻译用到的属性和方法就好了,其实这个就是UIView的数据源,而且动画都要用到这些数据,具体哪些我也不知道啊,还没看啊,有一些官网文档是需要看的。

需要阅读的文档:

                                                             View Programming Guide for iOS(link)-->

                                                             -->Drawing and Printing Guide for iOS(link)

                                                              -->Core Animation Programming Guide(link)

    

      肯定是不建议像我这样看文档开发啊,耗费时间代价太大了,不如直接百度来的快,但是阅读肯定也有很多好处啊,性价比很低而已,提高英语水平好吧。学会看官网文档好吧。

 

先看别人的介绍,很有用,参考链接:https://www.jianshu.com/p/ff8d4962ea63

 

以下内容大部分来源于

作者:Longshihua
链接:https://www.jianshu.com/p/ff8d4962ea63
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

● CALayer就是一个图层,可以让我们看见的东西,但是他不具有事件,为了让我们既能看得见又能操作,所以就在继承UIResponder来扩展了一个CALayer,这就是UIView,所以UIView里有一个CALayer的原因。

       所以,如果显示出来的东西需要跟用户进行交互的话,用UIView;如果不需要跟用户进行交互,用UIView或者CALayer都可以。

● CALayer本身并不包含在UIKit中,因此它不能响应事件

● 虽然CALayer可以使用frame,但最好还是使用bounds和position。为层设置动画时,用bounds和position会方便一点。而且frame本身不支持动画效果,通常使用bounds和position代替。
            sublayer的position只是父layer中的一个点而已,跟sublayer自身并没有关系,sublayer只是存储了父layer中的一个点而已。   sublayer自相情愿地把自己的anchorPoint绑定到自己存储的position中,设置现在sublayer的中下点为锚点,而存储的positon也存储为这里

● 隐式属性动画的本质是CALayer属性的变动,默认隐含了CABasicAnimation动画实现,所有的非Root Layer(也就是手动创建的CALayer对象)都存在着隐式动画.

● CALayer中透明度使用opacity表示而不是alpha;中心点使用position表示而不是center

● anchorPoint属性 是图层的锚点,范围在(0-1,0-1)表示在x、y轴的比例,这个点可以同position(原点)重合,当图层中心点固定后,调整anchorPoint即可达到调整图层显示位置的作用。锚点决定视图的移动,中心点决定一开始加载时,视图的位置,所以锚点用于动画效果时,性能会好很多。

      锚点也是坐标点,但是锚点是描述视图自身每一个点的坐标系,自己参考自己。就是说,锚点坐标系只能自己使用,别人不可用,别人用他自己的锚点坐标系。锚点坐标系不是用具体尺寸来描述的,而是用比例来描述的,所以锚点的x和y的取值范围都是0~1。

      比如说A视图的水平长度为100cm,B视图的水平长度为50cm,那么A的(0.2,0)锚点在A视图的20cm,B视图的(0.2,0)在B视图的10cm处,所以锚点是描述视图“自身相对于自身”的每一个坐标点。那么子视图是如何通过锚点在父视图中形变的呢?

      是这样,子视图刚刚放在父视图中的时候,子视图的中心点是父视图frame坐标系中的某一个点,我们就称父视图frame坐标系中的这个点为基准点,那么子视图自身的(0.5,0.5)锚点在初始化时默认是和这个基准点重合的,那么你在父视图中每设置一次子视图的锚点,就相当于把新设置的锚点移动到这个基准点处了,因为子视图的尺寸没有改变,所以就相当于把子视图移动了。  例如,把锚点(0.1,0.3)移动到这个基准点去,那么(0.5,0.5)锚点就不在这个基准点来了。

 

● 使用Core Animation有三个layer对象的集合,每一个集合对于内容显示在屏幕上都有着不同的作用和地位,对应的就是三棵树

  • 模型树(model tree)

        模型树是app中交互最多的树,模型树中存储着动画的目标值

  • 呈现树(presentation tree)

        呈现树是包含着动画执行过程中的值,可以获取但是不应该修改,

  • 渲染树(render tree)

        渲染树执行实际的动画,但是是私有的

     参考官网文档:Core Animation Programming Guide(link)
 

 

隐式动画

     每一个UIView内部都默认关联着一个CALayer,我们可称这个Layer为RootLayer(根层),所有的非RootLayer,也就是手动创建的CALayer对象,都存在着隐式动画。

      当对非RootLayer的部分属性进行修改时,默认会自动产生一些动画效果而这些属性称为Animatable Properties(可动画属性)


 

   

 

CALayer

     --An object that manages image-based content and allows you to perform animations on that content.

       一个管理 “基于图片的”视图内容的对象,就是一个管理“视图内容”的对象咯。用这个类可以很好的实现视图内容的动画效果,就是那很长的一堆属性和方法。               

 

Declaration

class CALayer : NSObject         //继承于UIKit最顶端的NSObject,够牛逼了吧

Overview

     --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.

       Layer通常当做view 的后备存库使用,但是Layer也可以独立于view来展示视图。Layer的主要工作是管理你提供的可视化内容,并且Layer自己也有一些可视化的属性,例如背景颜色、边框、阴影。

       除了管理可视化内容,Layer也包含了关于自身内容的几何信息,例如、可视内容的大小、位置、变形。这些几何信息就是用来在屏幕上展示视图的。所以,你定义动画效果也是通过修改Layer的这些几何属性来实现的。

       一个layer对象通过遵循CAMediaTiming 协议,封装了自身动画效果的持续时间和步调。而CAMediaTiming 协议,就是为定义layer的定时器而存在的。

 

     --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.

        如果layer对象是通过view来创建的,那么view默认将自身作为layer的委托对象,至于用其他对象作为layer的委托对象这么高端的操作就不用考虑了,我还没有那么高级。所以一般默认的这种委托关系绰绰有余了,就不要乱改了。

       但是,iOS也允许你设置其他对象作为layer的委托对象,这样你就可以动态的提供layer要展示的视图内容了,并且执行其他一些任务,不建议咯。一个layer对象也可以拥有布局管理对象,可以通过layer的layoutManager属性设置,这个布局管理对象主要用于独立地管理子视图的布局。

 

        至此,CALayer的概览就没了,没有什么使用建议,也没有什么使用注意事项,就一个overview,就没了。剩下的就是一大堆的属性和方法了。没那个时间逐个翻译那么一大堆属性和方法了,先看。用到再翻译了。很多属性和方法是我这种渣渣用不到的。


Creating a Layer                                                                                                                     -- 构造方法

init()

init(layer: Any)                   //构造方法:复制一个layer来创建一个layer

    --Override to copy or initialize custom fields of the specified layer.

        你可以复写该构造方法来创建一个layer啊,当然也可以在方法体里面做写自己的代码啊     

 

init(remoteClientId: UInt32)              //构造方法:通过远程id来初始化一个layer

    --Initializes a layer with a remote client ID.

 

Accessing Related Layer Objects                                                                       --  访问相关的布局对象

 

func presentation() -> Self?                                                  //方法: 返回当前屏幕展示的布局对象的副本

    --Returns a copy of the presentation layer object that represents the state of the layer as it currently appears onscreen.

 

func model() -> Self                                                                  //方法: 返回接收者模型层的布局对象的副本

    --Returns the model layer object associated with the receiver, if any.

 

Accessing the Delegate                                                                                             -- 访问layer的委托对象

var delegate: CALayerDelegate?                                            //计算属性:放回layer的委托对象

    --The layer’s delegate object.

 

Providing the Layer’s Content                                                                                             -- 提供layer的内容

var contents: Any?                                                                        //计算属性: 设置layer的内内容对象。可动画

    --An object that provides the contents of the layer. Animatable.

       一般用来设置静态图片的索引,一般不用你设置,view和layer交互时会自动经常修改这个属性。

 

var contentsRect: CGRect                                                      //属性:layer可用的内容的部分的矩形。可动画化

    --The rectangle, in the unit coordinate space, that defines the portion of the layer’s contents that should be used. Animatable.

       默认值为(0.0, 0.0, 1.0, 1.0).内容可超出矩形

 

var contentsCenter: CGRect                                                        //属性:layer可缩放的中心范围,比例设置。可动画化

    --The rectangle that defines how the layer contents are scaled if the layer’s contents are resized. Animatable.

       该矩将一个layer的内容划分为九宫格,每格的缩放限制有所区别。看api,必须与contentsGravity属性捆绑。

 

func display()                                                                                  //方法: 重加载layer的内容

    --Reloads the content of this layer.   

 

func draw(in: CGContext)                                                               //方法:用某中图形的上下文来绘制layer的内容

    --Draws the layer’s content using the specified graphics context.

 

Modifying the Layer’s Appearance                                                                -- 修改layer的外观

 

var contentsGravity: CALayerContentsGravity                        //常量属性:指定layer的内容的位置和缩放

    --A constant that specifies how the layer's contents are positioned or scaled within its bounds.

 

Contents Gravity Values                                                                            //文章:关于contentsGravity各个常量值的意义

    --The contents gravity constants specify the position of the content object when the layer bounds is larger than the bounds of the content object. They are used by the contentsGravity property.

 

var opacity: Float                                                                           //属性:不透明度,0为透明,1为不透明。可动画化

    --The opacity of the receiver. Animatable.

         理解为遮光度还更好理解一些

 

var isHidden: Bool                                                                              //属性:是否隐藏layer的显示

    --A Boolean indicating whether the layer is displayed. Animatable.

 

var masksToBounds: Bool                                                                  //属性:是否裁剪子layer的内容来适配父layer的边框范围

    --A Boolean indicating whether sublayers are clipped to the layer’s bounds. Animatable.

 

var mask: CALayer?                                            //属性:通过其它layer的透明通道,设置当前layer的透明度掩模

An optional layer whose alpha channel is used to mask the layer’s content.

 

var isDoubleSided: Bool                                         //属性:设置layer背对绑定的view的时候是否显示layer的后背

A Boolean indicating whether the layer displays its content when facing away from the viewer. Animatable.

 

var cornerRadius: CGFloat                                                                           //属性:设置layer的圆角(只对背景边框有效)

The radius to use when drawing rounded corners for the layer’s background. Animatable.

 

var maskedCorners: CACornerMask                                            //属性:我也不知道

 

struct CACornerMask                                                                                  //结构体属性:我也不知道

 

var borderWidth: CGFloat                                        //属性:设置layer边框条的宽度,boarder是根据bounds进行内绘制的

The width of the layer’s border. Animatable.//可动画化

 

var borderColor: CGColor?                                                                                                     //属性:设置边框条的颜色

The color of the layer’s border. Animatable.//可动画化

 

var backgroundColor: CGColor?                                                                                                   //属性:设置背景颜色

The background color of the receiver. Animatable.

 

var shadowOpacity: Float                                                                                            //属性:设置阴影的遮光度

The opacity of the layer’s shadow. Animatable.

 

var shadowRadius: CGFloat                                                                                            //属性:设置阴影的半径

The blur radius (in points) used to render the layer’s shadow. Animatable.

 

var shadowOffset: CGSize                                                                                           //属性:设置阴影的偏移量

The offset (in points) of the layer’s shadow. Animatable.

 

var shadowColor: CGColor?                                                                                   //属性:设置阴影的颜色

The color of the layer’s shadow. Animatable.

 

var shadowPath: CGPath?                                                                      //属性:设置阴影的绘制路径

The shape of the layer’s shadow. Animatable.

 

var style: [AnyHashable : Any]? 

                         //字典属性:用于临时存储未被layer明确定义的属性的值(不包含bounds和frame属性)

An optional dictionary used to store property values that aren't explicitly defined by the layer.

 

var allowsEdgeAntialiasing: Bool                                  //属性:设置是否“非锯齿”显示(即平滑显示)layer

A Boolean indicating whether the layer is allowed to perform edge antialiasing.

 

var allowsGroupOpacity: Bool                                                        //属性:设置自己的透明度是否可以和父图层分离,自成一组

A Boolean indicating whether the layer is allowed to composite itself as a group separate from its parent.

 

Layer Filters                                                                               -- layer的过滤器(图形处理知识中的过滤器)

 

var filters: [Any]?                                                          //数组属性:一组核心图片过滤器,iOS不支持

An array of Core Image filters to apply to the contents of the layer and its sublayers. Animatable.

 

var compositingFilter: Any?                                                   //属性:iOS不支持

A CoreImage filter used to composite the layer and the content behind it. Animatable.

 

var backgroundFilters: [Any]?                                          //属性:iOS不支持

An array of Core Image filters to apply to the content immediately behind the layer. Animatable.

 

var minificationFilter: CALayerContentsFilter                  //属性:减少layer内容大小时的缩小过滤器,默认是线性过滤器

The filter used when reducing the size of the content.

 

var minificationFilterBias: Float                                          //属性:缩小过滤器用来确定细节级别的偏移因子。

The bias factor used by the minification filter to determine the levels of detail.

 

var magnificationFilter: CALayerContentsFilter               //属性:增加layer内容大小时的缩小过滤器,默认是线性过滤器

The filter used when increasing the size of the content.

 

 

Configuring the Layer’s Rendering Behavior                                 -- 配置layer的渲染行为

 

var isOpaque: Bool                                                                                    //属性:设置layer的是否完全遮光

A Boolean value indicating whether the layer contains completely opaque content.

 

var edgeAntialiasingMask: CAEdgeAntialiasingMask              //属性:设置内容边缘的“非锯齿”显示的掩模

A bitmask defining how the edges of the receiver are rasterized.

 

func contentsAreFlipped() -> Bool                                       //方法:告知layer在渲染过程中是否发生了隐式翻转

Returns a Boolean indicating whether the layer content is implicitly flipped when rendered.

 

var isGeometryFlipped: Bool                                              //属性:设置是否使用翻转坐标系(false为标准坐标系)

A Boolean that indicates whether the geometry of the layer and its sublayers is flipped vertically.

 

var drawsAsynchronously: Bool                                                       //属性:设置绘制命令 是否 在后台 排队并且异步 执行

A Boolean indicating whether drawing commands are deferred and processed asynchronously in a background thread.

 

var shouldRasterize: Bool                                                 //属性:设置layer是否以位图的方式进行渲染,默认false

A Boolean that indicates whether the layer is rendered as a bitmap before compositing. Animatable

 

var rasterizationScale: CGFloat                                                              //属性:设置将layer进行栅格化的比例(位图)

The scale at which to rasterize content, relative to the coordinate space of the layer. Animatable

 

var contentsFormat: CALayerContentsFormat                                  //属性:设置储存layer的的储存格式的提示

A hint for the desired storage format of the layer contents.

 

func render(in: CGContext)                                                                   //方法:在指定的“图形上下文”中渲染layer

Renders the layer and its sublayers into the specified context.

 

 

Modifying the Layer Geometry                                        -- 修改layer的几何图形

 

var frame: CGRect

The layer’s frame rectangle.

 

var bounds: CGRect

The layer’s bounds rectangle. Animatable.

 

var position: CGPoint

The layer’s position in its superlayer’s coordinate space. Animatable.

 

var zPosition: CGFloat

The layer’s position on the z axis. Animatable.

 

var anchorPointZ: CGFloat

The anchor point for the layer’s position along the z axis. Animatable.

 

var anchorPoint: CGPoint

Defines the anchor point of the layer's bounds rectangle. Animatable.

 

var contentsScale: CGFloat

The scale factor applied to the layer.

 

Managing the Layer’s Transform                           -- 管理layer的形变

 

var transform: CATransform3D

The transform applied to the layer’s contents. Animatable.

 

var sublayerTransform: CATransform3D

Specifies the transform to apply to sublayers when rendering. Animatable.

 

func affineTransform() -> CGAffineTransform

Returns an affine version of the layer’s transform.

 

func setAffineTransform(CGAffineTransform)

Sets the layer’s transform to the specified affine transform.

 

Managing the Layer Hierarchy -- 管理layer的图层

 

var sublayers: [CALayer]?

An array containing the layer’s sublayers.

 

var superlayer: CALayer?

The superlayer of the layer.

 

func addSublayer(CALayer)

Appends the layer to the layer’s list of sublayers.

 

func removeFromSuperlayer()

Detaches the layer from its parent layer.

 

func insertSublayer(CALayer, at: UInt32)

Inserts the specified layer into the receiver’s list of sublayers at the specified index.

 

func insertSublayer(CALayer, below: CALayer?)

Inserts the specified sublayer below a different sublayer that already belongs to the receiver.

 

func insertSublayer(CALayer, above: CALayer?)

Inserts the specified sublayer above a different sublayer that already belongs to the receiver.

 

func replaceSublayer(CALayer, with: CALayer)

Replaces the specified sublayer with a different layer object.

 

Updating Layer Display                                            -- 更新layer的展示

 

func setNeedsDisplay()

Marks the layer’s contents as needing to be updated.

 

func setNeedsDisplay(CGRect)

Marks the region within the specified rectangle as needing to be updated.

 

var needsDisplayOnBoundsChange: Bool

A Boolean indicating whether the layer contents must be updated when its bounds rectangle changes.

 

func displayIfNeeded()

Initiates the update process for a layer if it is currently marked as needing an update.

 

func needsDisplay() -> Bool

Returns a Boolean indicating whether the layer has been marked as needing an update.

 

class func needsDisplay(forKey: String) -> Bool

Returns a Boolean indicating whether changes to the specified key require the layer to be redisplayed.

 

Layer Animations                                    -- layer的动画

 

func add(CAAnimation, forKey: String?)

Add the specified animation object to the layer’s render tree.

 

func animation(forKey: String) -> CAAnimation?

Returns the animation object with the specified identifier.

 

func removeAllAnimations()

Remove all animations attached to the layer.

 

func removeAnimation(forKey: String)

Remove the animation object with the specified key.

 

func animationKeys() -> [String]?

Returns an array of strings that identify the animations currently attached to the layer.

 

Managing Layer Resizing and Layout                           -- 管理layer的尺寸调整和布局

 

var layoutManager: CALayoutManager?

The object responsible for laying out the layer’s sublayers.

 

func setNeedsLayout()

Invalidates the layer’s layout and marks it as needing an update.

 

func layoutSublayers()

Tells the layer to update its layout.

 

func layoutIfNeeded()

Recalculate the receiver’s layout, if required.

 

func needsLayout() -> Bool

Returns a Boolean indicating whether the layer has been marked as needing a layout update.

 

var autoresizingMask: CAAutoresizingMask

A bitmask defining how the layer is resized when the bounds of its superlayer changes.

 

func resize(withOldSuperlayerSize: CGSize)

Informs the receiver that the size of its superlayer changed.

 

func resizeSublayers(withOldSize: CGSize)

Informs the receiver’s sublayers that the receiver’s size has changed.

 

func preferredFrameSize() -> CGSize

Returns the preferred size of the layer in the coordinate space of its superlayer.

 

Managing Layer Constraints                                     -- 管理layer的约束

 

var constraints: [CAConstraint]?

The constraints used to position current layer’s sublayers.

 

func addConstraint(CAConstraint)

Adds the specified constraint to the layer.

 

Getting the Layer’s Actions                               -- 获取layer的动作

 

func action(forKey: String) -> CAAction?

Returns the action object assigned to the specified key.

 

var actions: [String : CAAction]?

A dictionary containing layer actions.

 

class func defaultAction(forKey: String) -> CAAction?

Returns the default action for the current class.

 

Mapping Between Coordinate and Time Spaces -- 坐标与时间空间之间的映射

 

func convert(CGPoint, from: CALayer?) -> CGPoint

Converts the point from the specified layer’s coordinate system to the receiver’s coordinate system.

 

func convert(CGPoint, to: CALayer?) -> CGPoint

Converts the point from the receiver’s coordinate system to the specified layer’s coordinate system.

 

func convert(CGRect, from: CALayer?) -> CGRect

Converts the rectangle from the specified layer’s coordinate system to the receiver’s coordinate system.

 

func convert(CGRect, to: CALayer?) -> CGRect

Converts the rectangle from the receiver’s coordinate system to the specified layer’s coordinate system.

 

func convertTime(CFTimeInterval, from: CALayer?) -> CFTimeInterval

Converts the time interval from the specified layer’s time space to the receiver’s time space.

 

func convertTime(CFTimeInterval, to: CALayer?) -> CFTimeInterval

Converts the time interval from the receiver’s time space to the specified layer’s time space

 

Hit Testing             -- 点击测试

 

func hitTest(CGPoint) -> CALayer?

Returns the farthest descendant of the receiver in the layer hierarchy (including itself) that contains the specified point.

 

func contains(CGPoint) -> Bool

Returns whether the receiver contains a specified point.

 

Scrolling                                -- 滑动

 

var visibleRect: CGRect

The visible region of the layer in its own coordinate space.

 

func scroll(CGPoint)

Initiates a scroll in the layer’s closest ancestor scroll layer so that the specified point lies at the origin of the scroll layer.

 

func scrollRectToVisible(CGRect)

Initiates a scroll in the layer’s closest ancestor scroll layer so that the specified rectangle becomes visible.

 

Identifying the Layer                                     -- layer的标识

 

var name: String?

The name of the receiver.

 

Key-Value Coding Extensions                             -- key-value编码拓展

 

func shouldArchiveValue(forKey: String) -> Bool

Returns a Boolean indicating whether the value of the specified key should be archived.

 

class func defaultValue(forKey: String) -> Any?

Specifies the default value associated with the specified key.

 

Constants                                        -- 一些常量

struct CAAutoresizingMask

These constants are used by the autoresizingMask property.

 

Action Identifiers

These constants are the predefined action identifiers used by action(forKey:), add(_:forKey:), defaultAction(forKey:),

removeAnimation(forKey:), Layer Filters, and the CAAction protocol method run(forKey:object:arguments:).

 

struct CAEdgeAntialiasingMask

This mask is used by the edgeAntialiasingMask property.

 

Identity Transform

Defines the identity transform matrix used by Core Animation.

 

Scaling Filters

These constants specify the scaling filters used by magnificationFilter and minificationFilter.

 

struct CATransform3D

The standard transform matrix used throughout Core Animation.

 

Instance Properties                       -- 实例属性

var cornerCurve: CALayerCornerCurve

 

Type Methods                     -- 静态方法

class func cornerCurveExpansionFactor(CALayerCornerCurve) -> CGFloat

 

Relationships

Inherits From

NSObject

Conforms To

  • CAMediaTiming

  • CVarArg

  • Equatable

  • Hashable

  • NSSecureCoding

See Also

Layer Basics

protocol CALayerDelegate

Methods your app can implement to respond to layer-related events.

class CAConstraint

A representation of a single layout constraint between two layers.

protocol CALayoutManager

Methods that allow an object to manage the layout of a layer and its sublayers.

class CAConstraintLayoutManager

An object that provides a constraint-based layout manager.

protocol CAAction

An interface that allows objects to respond to actions triggered by a CALayer change.

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(IOS,API阅读,Animation,动画相关)