CALayer 方法属性整理
/*创建并初始化*/ + (instancetype)layer; /*这个初始化方法被coreAnimation 用来创建layer 的阴影 */ - (instancetype)initWithLayer:(id)layer; /*返回当前正在显示的呈现图层, 动画被激活之后调用此方法才有效*/ - (nullable id)presentationLayer; /*key 为需要动画的属性, 如果该属性需要自定义动画,则调用测方法返回 YES 即可. 此方法被子类重写. 对于给定的属性返回 YES, 如果该属性的值发生改变(包括附加在 layer 上的动画使该属性的值发生改变),则该属性所在的图层将会进行重绘. 如果此方法指定的属性没有发生改变, 则应调用父类的此方法[super needsDisplayForKey:key]. */ + (BOOL)needsDisplayForKey:(NSString *)key; /*可动画属性: 默认为CGRectZero, layer 的大小*/ @property CGRect bounds;
/*可动画属性, layer 的锚点在 superLayer 中的位置, 锚点默认在试图的中点位置*/ @property CGPoint position;
/*layer 在 superLayer 中z 方向图层树的位置, 默认为0, 可动画*/ @property CGFloat zPosition; /*锚点: 图层的定点, 相对于图层本身的单位点, 可动画属性*/ @property CGPoint anchorPoint; /*应用于 layer 的形变(transform), 依赖于锚点, 默认为 identityTransform, 可动画属性*/ @property CATransform3D transform; /*便利方法 对于访问 layer 的 transform 属性 CGAffineTransform类型*/ - (CGAffineTransform)affineTransform; - (void)setAffineTransform:(CGAffineTransform)m; /*隐藏 layer , 不可动画*/ @property(getter=isHidden) BOOL hidden; /*图层可以双面渲染 , 可动画属性*/ @property(getter=isDoubleSided) BOOL doubleSided; /*父图层*/ @property(nullable, readonly) CALayer *superlayer; /*将 layer 从它的父视图中移除, 接受对象:1. layer 在父视图的子视图数组中,; 2. layer 设置为父视图的 mask 属性*/ - (void)removeFromSuperlayer; /*将 layer 作为子视图插入到 接收者的subLayers 中制定的位置 idx, 如果 layer 有父视图, 则会先将 layer 从父视图中移除, 再插入到新的父视图中的位置 idx*/ - (void)insertSublayer:(CALayer *)layer atIndex:(unsigned)idx; /* layer: 被插入的对象, receiver: 将要插入到的父视图 sibling: 在父视图中已经存在的子视图, 若为 nil (below:将 layer 插入到第一个位置, above: 插入到最后一个位置. 将 layer 插入到父视图中 sibling 的前面(below)或者后面(above:被当前显示出来的), 插入前会将 layer 从它的父视图中移除 */ - (void)insertSublayer:(CALayer *)layer below:(nullable CALayer *)sibling; - (void)insertSublayer:(CALayer *)layer above:(nullable CALayer *)sibling; /*遮罩属性, layer 定义形状, 父视图定义显示的内容*/ @property(nullable, strong) CALayer *mask;
/*返回 YES 时, 会剪切边界,*/
@property BOOL masksToBounds;
/** Mapping between layer coordinate and time spaces. **/
/*点/时间 或者 Rect 在不同坐标系之间的转换*/
- (CGPoint)convertPoint:(CGPoint)p fromLayer:(nullable CALayer *)l;
- (CGPoint)convertPoint:(CGPoint)p toLayer:(nullable CALayer *)l;
- (CGRect)convertRect:(CGRect)r fromLayer:(nullable CALayer *)l;
- (CGRect)convertRect:(CGRect)r toLayer:(nullable CALayer *)l;
- (CFTimeInterval)convertTime:(CFTimeInterval)t fromLayer:(nullable CALayer *)l;
- (CFTimeInterval)convertTime:(CFTimeInterval)t toLayer:(nullable CALayer *)l;
/** Hit testing methods. **/
/*判断点 p 是否在当前 layer 的坐标系中, 在则返回 layer , 不在则返回 nil*/
- (nullable CALayer *)hitTest:(CGPoint)p;
/* Returns true if the bounds of the layer contains point 'p'. */
- (BOOL)containsPoint:(CGPoint)p;
/** Layer content properties and methods. **/
/* An object providing the contents of the layer, typically a CGImageRef,
* but may be something else. (For example, NSImage objects are
* supported on Mac OS X 10.6 and later.) Default value is nil.
* Animatable. */
/*一般用来设置 layer 的图片*/
@property(nullable, strong) id contents;
/* Reload the content of this layer. Calls the -drawInContext: method
* then updates the `contents' property of the layer. Typically this is
* not called directly. */
/*调用此方法会执行 -drawInContext: 方法, 来更新layer 的 contents 属性, 一般不直接调用此方法*/
- (void)display;
/* Marks that -display needs to be called before the layer is next
* committed. If a region is specified, only that region of the layer
* is invalidated. */
/*UIview 的 setNeedsDisplay 和 setNeedsLayout 方法区别
1. 两个方法都是异步执行的, 而 setNeedsDisplay 方法会自动调用 drawRect 方法, 这样可以拿到当前的 Context, 就能对 layer 进行重新绘制
2. setNeedsLayout 会调用 layoutSubViews 方法, 进行对子控件的重新布局.
*/
- (void)setNeedsDisplay;
- (void)setNeedsDisplayInRect:(CGRect)r;
/* Returns true when the layer is marked as needing redrawing. */
/*标记 layer 需要进行重新绘制*/
- (BOOL)needsDisplay;
/* Call -display if receiver is marked as needing redrawing. */
/*回调 -displayer 方法, 如果接收者需要进行重绘*/
- (void)displayIfNeeded;
/* When true -setNeedsDisplay will automatically be called when the
* bounds of the layer changes. Default value is NO. */
@property BOOL needsDisplayOnBoundsChange;/** Rendering properties and methods. **/
/* Renders the receiver and its sublayers into 'ctx'. This method
* renders directly from the layer tree. Renders in the coordinate space
* of the layer.
*
* WARNING: currently this method does not implement the full
* CoreAnimation composition model, use with caution. */
- (void)renderInContext:(CGContextRef)ctx;/* The background color of the layer. Default value is nil. Colors
* created from tiled patterns are supported. Animatable. */
/*设置背景颜色*/
@property(nullable) CGColorRef backgroundColor;
/* When positive, the background of the layer will be drawn with
* rounded corners. Also effects the mask generated by the
* `masksToBounds' property. Defaults to zero. Animatable. */
/*角半径: 设置圆角*/
@property CGFloat cornerRadius;
/* The width of the layer's border, inset from the layer bounds. The
* border is composited above the layer's content and sublayers and
* includes the effects of the `cornerRadius' property. Defaults to
* zero. Animatable. */
/*设置 layer 的边界宽度*/
@property CGFloat borderWidth;
/* The color of the layer's border. Defaults to opaque black. Colors
* created from tiled patterns are supported. Animatable. */
/*设置边界的颜色, 可动画*/
@property(nullable) CGColorRef borderColor;
/* The opacity of the layer, as a value between zero and one. Defaults
* to one. Specifying a value outside the [0,1] range will give undefined
* results. Animatable. */
/*layer 的透明度, 可动画*/
@property float opacity;/** Shadow properties. **/
/* The color of the shadow. Defaults to opaque black. Colors created
* from patterns are currently NOT supported. Animatable. */
/*阴影颜色*/
@property(nullable) CGColorRef shadowColor;
/* The opacity of the shadow. Defaults to 0. Specifying a value outside the
* [0,1] range will give undefined results. Animatable. */
/*阴影透明度*/
@property float shadowOpacity;
/* The shadow offset. Defaults to (0, -3). Animatable. */
/*阴影偏移量*/
@property CGSize shadowOffset;
/* The blur radius used to create the shadow. Defaults to 3. Animatable. */
/*阴影圆角属性*/
@property CGFloat shadowRadius;
/* When non-null this path defines the outline used to construct the
* layer's shadow instead of using the layer's composited alpha
* channel. The path is rendered using the non-zero winding rule.
* Specifying the path explicitly using this property will usually
* improve rendering performance, as will sharing the same path
* reference across multiple layers. Upon assignment the path is copied.
* Defaults to null. Animatable. */
/*自定义阴影的形状*/
@property(nullable) CGPathRef shadowPath;
/** Layout methods. **//* Marks that -layoutSublayers needs to be invoked on the receiver
* before the next update. If the receiver's layout manager implements
* the -invalidateLayoutOfLayer: method it will be called.
*
* This method is automatically invoked on a layer whenever its
* `sublayers' or `layoutManager' property is modified, and is invoked
* on the layer and its superlayer whenever its `bounds' or `transform'
* properties are modified. Implicit calls to -setNeedsLayout are
* skipped if the layer is currently executing its -layoutSublayers
* method. *//**/
- (void)setNeedsLayout;
/* Returns true when the receiver is marked as needing layout. */
- (BOOL)needsLayout;
/* Traverse upwards from the layer while the superlayer requires layout.
* Then layout the entire tree beneath that ancestor. */
- (void)layoutIfNeeded;
/* Called when the layer requires layout. The default implementation
* calls the layout manager if one exists and it implements the
* -layoutSublayersOfLayer: method. Subclasses can override this to
* provide their own layout algorithm, which should set the frame of
* each sublayer. */
/*当 layer 需要布局的时候调用此方法.*/
- (void)layoutSublayers;
/** Action methods. **/
/* An "action" is an object that responds to an "event" via the
* CAAction protocol (see below). Events are named using standard
* dot-separated key paths. Each layer defines a mapping from event key
* paths to action objects. Events are posted by looking up the action
* object associated with the key path and sending it the method
* defined by the CAAction protocol.
*
* When an action object is invoked it receives three parameters: the
* key path naming the event, the object on which the event happened
* (i.e. the layer), and optionally a dictionary of named arguments
* specific to each event.
*
* To provide implicit animations for layer properties, an event with
* the same name as each property is posted whenever the value of the
* property is modified. A suitable CAAnimation object is associated by
* default with each implicit event (CAAnimation implements the action
* protocol).
*
* The layer class also defines the following events that are not
* linked directly to properties:
* onOrderIn
* Invoked when the layer is made visible, i.e. either its
* superlayer becomes visible, or it's added as a sublayer of a
* visible layer
* onOrderOut
* Invoked when the layer becomes non-visible. */
/* Returns the default action object associated with the event named by
* the string 'event'. The default implementation returns a suitable
* animation object for events posted by animatable properties, nil
* otherwise. */
+ (nullable id<CAAction>)defaultActionForKey:(NSString *)event;
/* Returns the action object associated with the event named by the
* string 'event'. The default implementation searches for an action
* object in the following places:
*
* 1. if defined, call the delegate method -actionForLayer:forKey:
* 2. look in the layer's `actions' dictionary
* 3. look in any `actions' dictionaries in the `style' hierarchy
* 4. call +defaultActionForKey: on the layer's class
*
* If any of these steps results in a non-nil action object, the
* following steps are ignored. If the final result is an instance of
* NSNull, it is converted to `nil'. */
- (nullable id<CAAction>)actionForKey:(NSString *)event;
/* A dictionary mapping keys to objects implementing the CAAction
* protocol. Default value is nil. */
@property(nullable, copy) NSDictionary<NSString *, id<CAAction>> *actions;
/** Animation methods. **/
/* Attach an animation object to the layer. Typically this is implicitly
* invoked through an action that is an CAAnimation object.
*
* 'key' may be any string such that only one animation per unique key
* is added per layer. The special key 'transition' is automatically
* used for transition animations. The nil pointer is also a valid key.
*
* If the `duration' property of the animation is zero or negative it
* is given the default duration, either the value of the
* `animationDuration' transaction property or .25 seconds otherwise.
*
* The animation is copied before being added to the layer, so any
* subsequent modifications to `anim' will have no affect unless it is
* added to another layer. */
- (void)addAnimation:(CAAnimation *)anim forKey:(nullable NSString *)key;
/* Remove all animations attached to the layer. */
- (void)removeAllAnimations;
/* Remove any animation attached to the layer for 'key'. */
- (void)removeAnimationForKey:(NSString *)key;
/* Returns an array containing the keys of all animations currently
* attached to the receiver. The order of the array matches the order
* in which animations will be applied. */
- (nullable NSArray<NSString *> *)animationKeys;
/* Returns the animation added to the layer with identifier 'key', or nil
* if no such animation exists. Attempting to modify any properties of
* the returned object will result in undefined behavior. */
- (nullable CAAnimation *)animationForKey:(NSString *)key;
/** Miscellaneous properties. **/
/* The name of the layer. Used by some layout managers. Defaults to nil. */
@property(nullable, copy) NSString *name;
/* An object that will receive the CALayer delegate methods defined
* below (for those that it implements). The value of this property is
* not retained. Default value is nil. */
@property(nullable, weak) id delegate;
/* When non-nil, a dictionary dereferenced to find property values that
* aren't explicitly defined by the layer. (This dictionary may in turn
* have a `style' property, forming a hierarchy of default values.)
* If the style dictionary doesn't define a value for an attribute, the
* +defaultValueForKey: method is called. Defaults to nil.
*
* Note that if the dictionary or any of its ancestors are modified,
* the values of the layer's properties are undefined until the `style'
* property is reset. */
@property(nullable, copy) NSDictionary *style;
@end
/** Action (event handler) protocol. **/
@protocol CAAction
/* Called to trigger the event named 'path' on the receiver. The object
* (e.g. the layer) on which the event happened is 'anObject'. The
* arguments dictionary may be nil, if non-nil it carries parameters
* associated with the event. */
- (void)runActionForKey:(NSString *)event object:(id)anObject
arguments:(nullable NSDictionary *)dict;
@end
/** NSNull protocol conformance. **/
@interface NSNull (CAActionAdditions) <CAAction>
@end
/** Delegate methods. **/
@interface NSObject (CALayerDelegate)
/* If defined, called by the default implementation of the -display
* method, in which case it should implement the entire display
* process (typically by setting the `contents' property). */
- (void)displayLayer:(CALayer *)layer;
/* If defined, called by the default implementation of -drawInContext: */
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx;
/* Called by the default -layoutSublayers implementation before the layout
* manager is checked. Note that if the delegate method is invoked, the
* layout manager will be ignored. */
- (void)layoutSublayersOfLayer:(CALayer *)layer;
/* If defined, called by the default implementation of the
* -actionForKey: method. Should return an object implementating the
* CAAction protocol. May return 'nil' if the delegate doesn't specify
* a behavior for the current event. Returning the null object (i.e.
* '[NSNull null]') explicitly forces no further search. (I.e. the
* +defaultActionForKey: method will not be called.) */
- (nullable id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event;