/* CoreAnimation - CALayer.h Copyright (c) 2006-2017, Apple Inc. All rights reserved. */ #import <QuartzCore/CAMediaTiming.h> #import <QuartzCore/CATransform3D.h> #import <Foundation/NSObject.h> #import <Foundation/NSNull.h> #import <Foundation/NSArray.h> #import <Foundation/NSDictionary.h> @class NSEnumerator, CAAnimation, CALayerArray; @protocol CAAction, CALayerDelegate; NS_ASSUME_NONNULL_BEGIN /* Bit definitions for `edgeAntialiasingMask' property. */ typedef NS_OPTIONS (unsigned int, CAEdgeAntialiasingMask) { kCALayerLeftEdge = 1U << 0, /* Minimum X edge. */ kCALayerRightEdge = 1U << 1, /* Maximum X edge. */ kCALayerBottomEdge = 1U << 2, /* Minimum Y edge. */ kCALayerTopEdge = 1U << 3, /* Maximum Y edge. */ }; /* Bit definitions for `maskedCorners' property. */ typedef NS_OPTIONS (NSUInteger, ) { kCALayerMinXMinYCorner = 1U << 0, kCALayerMaxXMinYCorner = 1U << 1, kCALayerMinXMaxYCorner = 1U << 2, kCALayerMaxXMaxYCorner = 1U << 3, }; /** The base layer class. **/ CA_CLASS_AVAILABLE (10.5, 2.0, 9.0, 2.0) @interface CALayer : NSObject <NSSecureCoding, CAMediaTiming> { @private struct _CALayerIvars { int32_t refcount; uint32_t magic; void *layer; #if TARGET_OS_MAC && !TARGET_RT_64_BIT void * _Nonnull unused1[8]; #endif } _attr; } /** Layer creation and initialization. **/ //初始化方法 + (instancetype)layer; - (instancetype)init; - (instancetype)initWithLayer:(id)layer; //presentationLayer是通過modelLayer提供屬性進行繪制的 //呈現層 用於顯示動畫 - (nullable instancetype)presentationLayer; //模型層 設置的layer屬性就是在這個層 - (instancetype)modelLayer; /** Property methods. **/ //重寫修改CALayer或其子類的默認值 ,key為屬性名稱,如果沒有該屬性返回nil + (nullable id)defaultValueForKey:(NSString *)key; //需要子類重寫,當CALayer或其子類屬性被修改時調用此方法 //key為修改的屬性名,返回YES是系統自動調用-display方法 + (BOOL)needsDisplayForKey:(NSString *)key; //指示指定鍵的值是否應該歸檔。 - (BOOL)shouldArchiveValueForKey:(NSString *)key; //layer的大小 @property CGRect bounds; //設置CALayer在父層中的位置 @property CGPoint position; //z方向的位置 @property CGFloat zPosition; //平面上的錨點(0-1),決定layer身上的哪個點在position的位置上 @property CGPoint anchorPoint; //z方向上的錨點 @property CGFloat anchorPointZ; //矩陣 仿射變換 @property CATransform3D transform; //返回和設置仿射變換值 - (CGAffineTransform)affineTransform; - (void)setAffineTransform:(CGAffineTransform)m; //設置layer的坐標 @property CGRect frame; //是否隱藏 @property(getter=isHidden) BOOL hidden; //設置layer的背面是否被繪制,默認是YES,NO時當圖層從相機視角消失是將不會被繪制。 @property(getter=isDoubleSided) BOOL doubleSided; //默認為NO,設置為YES所有子圖層相對於左上角的位置改為相對於左下角位置 @property(getter=isGeometryFlipped) BOOL geometryFlipped; //獲取當前layer內容在Y軸方向是否被翻轉了 - (BOOL)contentsAreFlipped; //獲取父層layer @property(nullable, readonly) CALayer *superlayer; //從父層layer上移除 - (void)removeFromSuperlayer; //獲取所有子layer數組 @property(nullable, copy) NSArray<CALayer *> *sublayers; //將layer添加到父視圖 - (void)addSublayer:(CALayer *)layer; //插入一個layer - (void)insertSublayer:(CALayer *)layer atIndex:(unsigned)idx; - (void)insertSublayer:(CALayer *)layer below:(nullable CALayer *)sibling; - (void)insertSublayer:(CALayer *)layer above:(nullable CALayer *)sibling; //用layer2替換layer - (void)replaceSublayer:(CALayer *)layer with:(CALayer *)layer2; //對子layer進行3D變換 @property CATransform3D sublayerTransform; //圖層蒙版layer @property(nullable, strong) CALayer *mask; //是否進行bounds切割,通常設置圓角時使用 @property BOOL masksToBounds; /** Mapping between layer coordinate and time spaces. **/ //轉換坐標點、矩形 - (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; //轉換CAMediaTiming協議的相對時間 - (CFTimeInterval)convertTime:(CFTimeInterval)t fromLayer:(nullable CALayer *)l; - (CFTimeInterval)convertTime:(CFTimeInterval)t toLayer:(nullable CALayer *)l; /** Hit testing methods. **/ //返回包含某一點的最上層的子layer - (nullable CALayer *)hitTest:(CGPoint)p; //判斷layer是否包含某一點 - (BOOL)containsPoint:(CGPoint)p; /** Layer content properties and methods. **/ //設置layer的內容 ,一般設置CGImage的對象 @property(nullable, strong) id contents; //獲取內容的rect尺寸 @property CGRect contentsRect; //設置內容對齊和填充方式(枚舉) @property(copy) NSString *contentsGravity; //設置內容的縮放 @property CGFloat contentsScale CA_AVAILABLE_STARTING (10.7, 4.0, 9.0, 2.0); //設置一個矩形變形區域默認(0,0,1,1)取值0-1 @property CGRect contentsCenter; //設置內容存儲格式 @property(copy) NSString *contentsFormat CA_AVAILABLE_STARTING (10.12, 10.0, 10.0, 3.0); //設置縮小和放大模式 @property(copy) NSString *minificationFilter; @property(copy) NSString *magnificationFilter; //縮放因子 @property float minificationFilterBias; //設置內容完全不透明 默認NO @property(getter=isOpaque) BOOL opaque; //重新加載繪制內容 - (void)display; // 設置內容或某一區域內容加載 - (void)setNeedsDisplay; - (void)setNeedsDisplayInRect:(CGRect)r; //獲取是否需要重新繪制 - (BOOL)needsDisplay; //如果需要進行內容重新繪制 - (void)displayIfNeeded; //設置為YES 內容改變后自動調用 - (void)setNeedsDisplay;方法 @property BOOL needsDisplayOnBoundsChange; //延遲繪制命令,用於需要頻繁重繪的視圖 @property BOOL drawsAsynchronously CA_AVAILABLE_STARTING (10.8, 6.0, 9.0, 2.0); //繪制內容 - (void)drawInContext:(CGContextRef)ctx; /** Rendering properties and methods. **/ //讀取內容 - (void)renderInContext:(CGContextRef)ctx; //用於限定層的邊緣如何柵格化,默認是抗鋸齒 @property CAEdgeAntialiasingMask edgeAntialiasingMask; //YES時,要求edgeAntialiasingMask屬性邊抗鋸齒,默認值是從Info.plist中UIViewEdgeAntialiasing屬性獲取,如果沒有值默認NO @property BOOL allowsEdgeAntialiasing; //設置layer背景色 @property(nullable) CGColorRef backgroundColor; //設置layer圓角半徑 @property CGFloat cornerRadius; //設置layer哪幾個角顯示圓角 支持動畫 @property CACornerMask maskedCorners CA_AVAILABLE_STARTING (10.13, 11.0, 11.0, 4.0); //設置邊框寬度 @property CGFloat borderWidth; //設置邊框顏色 @property(nullable) CGColorRef borderColor; //設置透明度 @property float opacity; //默認YES(iOS7之后)子layer最高透明度上限是父layer的透明度,NO各自控制 @property BOOL allowsGroupOpacity; //一個coreImage過濾器 用於合成圖層及其后面內容 可動畫 @property(nullable, strong) id compositingFilter; //應用於圖層和其子圖層內容的coreImage過濾器數組 @property(nullable, copy) NSArray *filters; //應用於圖層后面的內容的coreImage過濾器數組 可動畫 @property(nullable, copy) NSArray *backgroundFilters; //光柵化 @property BOOL shouldRasterize; //當shouldRasterize設置為YES是,也就是光柵化之后 設置此屬性防止retina屏幕像素化(默認值1) @property CGFloat rasterizationScale; /** Shadow properties. **/ //陰影顏色 可動畫 @property(nullable) CGColorRef shadowColor; //陰影透明度 可動畫 @property float shadowOpacity; //陰影偏移量 可動畫 @property CGSize shadowOffset; //陰影圓角半徑 可動畫 @property CGFloat shadowRadius; //陰影路徑 可動畫 @property(nullable) CGPathRef shadowPath; /** Layout methods. **/ //返回圖層在其父圖層的坐標空間中的首選大小 - (CGSize)preferredFrameSize; //使圖層無效,標記需要更新 - (void)setNeedsLayout; //返回是否需要布局更新標志 - (BOOL)needsLayout; //如果需要,重新計算布局 - (void)layoutIfNeeded; //重新布局所有子layer - (void)layoutSublayers; /** Action methods. **/ //返回當前類的默認操作 + (nullable id<CAAction>)defaultActionForKey:(NSString *)event; //返回分配給指定鍵的動作對象 - (nullable id<CAAction>)actionForKey:(NSString *)event; //包含圖層動作的字典 @property(nullable, copy) NSDictionary<NSString *, id<CAAction>> *actions; /** Animation methods. **/ //給layer渲染樹添加指定動畫對象 - (void)addAnimation:(CAAnimation *)anim forKey:(nullable NSString *)key; //移除所有動畫對象 - (void)removeAllAnimations; //移除指定動畫對象 - (void)removeAnimationForKey:(NSString *)key; //返回字符串數組,標記當前layer附加的所有動畫 - (nullable NSArray<NSString *> *)animationKeys; //通過指定的標識返回一個動畫對象 - (nullable CAAnimation *)animationForKey:(NSString *)key; /** Miscellaneous properties. **/ //圖層標識 @property(nullable, copy) NSString *name; //代理 @property(nullable, weak) id <CALayerDelegate> delegate; //一個用於存儲未由圖層顯示定義的屬性值的可選字典 @property(nullable, copy) NSDictionary *style; @end /** Action (event handler) protocol. **/ @protocol CAAction //CAAction協議方法(action對象 隱式動畫) - (void)runActionForKey:(NSString *)event object:(id)anObject arguments:(nullable NSDictionary *)dict; @end /** NSNull protocol conformance. **/ @interface NSNull (CAActionAdditions) <CAAction> @end /** Delegate methods. **/ @protocol CALayerDelegate <NSObject> @optional //更新圖層時回調 - (void)displayLayer:(CALayer *)layer; //繪制圖層時回調 - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx; //圖層將要繪制是回調 - (void)layerWillDraw:(CALayer *)layer CA_AVAILABLE_STARTING (10.12, 10.0, 10.0, 3.0); //子layer布局時回調 - (void)layoutSublayersOfLayer:(CALayer *)layer; //執行指定操作時回調 - (nullable id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event; @end /** Layer `contentsGravity' values. **/ CA_EXTERN NSString * const kCAGravityCenter CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0); CA_EXTERN NSString * const kCAGravityTop CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0); CA_EXTERN NSString * const kCAGravityBottom CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0); CA_EXTERN NSString * const kCAGravityLeft CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0); CA_EXTERN NSString * const kCAGravityRight CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0); CA_EXTERN NSString * const kCAGravityTopLeft CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0); CA_EXTERN NSString * const kCAGravityTopRight CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0); CA_EXTERN NSString * const kCAGravityBottomLeft CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0); CA_EXTERN NSString * const kCAGravityBottomRight CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0); CA_EXTERN NSString * const kCAGravityResize CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0); CA_EXTERN NSString * const kCAGravityResizeAspect CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0); CA_EXTERN NSString * const kCAGravityResizeAspectFill CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0); /** Layer `contentsFormat` values. **/ CA_EXTERN NSString * const kCAContentsFormatRGBA8Uint /* RGBA UInt8 per component */ CA_AVAILABLE_STARTING (10.12, 10.0, 10.0, 3.0); CA_EXTERN NSString * const kCAContentsFormatRGBA16Float /* RGBA half-float 16-bit per component */ CA_AVAILABLE_STARTING (10.12, 10.0, 10.0, 3.0); CA_EXTERN NSString * const kCAContentsFormatGray8Uint /* Grayscale with alpha (if not opaque) UInt8 per component */ CA_AVAILABLE_STARTING (10.12, 10.0, 10.0, 3.0); /** Contents filter names. **/ CA_EXTERN NSString * const kCAFilterNearest CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0); CA_EXTERN NSString * const kCAFilterLinear CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0); /* Trilinear minification filter. Enables mipmap generation. Some * renderers may ignore this, or impose additional restrictions, such * as source images requiring power-of-two dimensions. */ CA_EXTERN NSString * const kCAFilterTrilinear CA_AVAILABLE_STARTING (10.6, 3.0, 9.0, 2.0); /** Layer event names. **/ CA_EXTERN NSString * const kCAOnOrderIn CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0); CA_EXTERN NSString * const kCAOnOrderOut CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0); /** The animation key used for transitions. **/ CA_EXTERN NSString * const kCATransition CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0); NS_ASSUME_NONNULL_END