CALayer

一个对象,该对象管理基于图像的内容,并允许你对该内容执行动画。

定义

  1. class CALayer : NSObject

概述

Layer通常用于为View提供后备存储,但是也可以在没有View的情况下使用Layer来显示内容。Layer的主要工作是管理你提供的视觉内容,但是Layer本身具有可以设置的视觉属性,例如背景颜色,边框和阴影。除了管理视觉内容之外,Layer还维护有关其内容的几何形状(例如其位置,大小和变换)的信息,这些信息用于在屏幕上呈现该内容。修改Layer的属性是在Layer的内容或几何图形上启动动画的方式。Layer对象采用CAMediaTiming协议来封装Layer及其动画的持续时间和步调,该协议定义了Layer的计时信息。

如果Layer对象是由View创建的,则View通常会自动将其自身分配为LayerDelegate,并且你不应更改该关系。对于你自己创建的图层,你可以分配一Delegate对象,并使用该对象动态提供Layer的内容并执行其他任务。Layer还可以具有一个布局管理器对象(分配给layoutManager属性),以分别管理子视图的布局。

主题

创建一个Layer

访问相关的Layer对象

访问Delegate

提供Layer的内容

更改Layer的显示

Layer过滤器

  • [var filters: [Any]? An array of Core Image filters to apply to the contents of the layer and its sublayers. Animatable.

  • [var compositingFilter: Any? A CoreImage filter used to composite the layer and the content behind it. Animatable.

  • [var backgroundFilters: [Any]? An array of Core Image filters to apply to the content immediately behind the layer. Animatable.

  • [var minificationFilter: CALayerContentsFilter 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 The filter used when increasing the size of the content.

配置Layer渲染行为

修改Layer几何形状

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

管理Layer层次结构

更新Layer的显示

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.

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

管理Layer约束

  • [var constraints: [CAConstraint]? The constraints used to position current layer’s sublayers.

  • [func addConstraint(CAConstraint) Adds the specified constraint to the layer.

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

坐标和时空之间的映射

  • [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

命中测试

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

滚动

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

Layer的标识

  • [var name: String? The name of the receiver.

键值编码扩展

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

常量

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

实例属性

  • [var cornerCurve: CALayerCornerCurve

类方法

  • [class func cornerCurveExpansionFactor(CALayerCornerCurve) -> CGFloat