版本記錄
| 版本號(hào) | 時(shí)間 |
|---|---|
| V1.0 | 2018.01.17 |
前言
CAShapeLayer類是我們經(jīng)常用到的類,平時(shí)也就用一下其基本用法,但是很少詳細(xì)的翻出來它的開發(fā)文檔仔細(xì)研究下,下面,我就帶領(lǐng)大家詳細(xì)的研究下這個(gè)類,詳細(xì)的看一下它的用法。
API文檔
這個(gè)類繼承自CALayer,屬于QuartzCore框架的類,下面我們就先看一下它的API。
/* CoreAnimation - CAShapeLayer.h
Copyright (c) 2008-2017, Apple Inc.
All rights reserved. */
#import <QuartzCore/CALayer.h>
NS_ASSUME_NONNULL_BEGIN
/* The shape layer draws a cubic Bezier spline in its coordinate space.
*
* The spline is described using a CGPath object and may have both fill
* and stroke components (in which case the stroke is composited over
* the fill). The shape as a whole is composited between the layer's
* contents and its first sublayer.
*
* The path object may be animated using any of the concrete subclasses
* of CAPropertyAnimation. Paths will interpolate as a linear blend of
* the "on-line" points; "off-line" points may be interpolated
* non-linearly (e.g. to preserve continuity of the curve's
* derivative). If the two paths have a different number of control
* points or segments the results are undefined.
*
* The shape will be drawn antialiased, and whenever possible it will
* be mapped into screen space before being rasterized to preserve
* resolution independence. (However, certain kinds of image processing
* operations, e.g. CoreImage filters, applied to the layer or its
* ancestors may force rasterization in a local coordinate space.)
*
* Note: rasterization may favor speed over accuracy, e.g. pixels with
* multiple intersecting path segments may not give exact results. */
CA_CLASS_AVAILABLE (10.6, 3.0, 9.0, 2.0)
@interface CAShapeLayer : CALayer
/* The path defining the shape to be rendered. If the path extends
* outside the layer bounds it will not automatically be clipped to the
* layer, only if the normal layer masking rules cause that. Upon
* assignment the path is copied. Defaults to null. Animatable.
* (Note that although the path property is animatable, no implicit
* animation will be created when the property is changed.) */
@property(nullable) CGPathRef path;
/* The color to fill the path, or nil for no fill. Defaults to opaque
* black. Animatable. */
@property(nullable) CGColorRef fillColor;
/* The fill rule used when filling the path. Options are `non-zero' and
* `even-odd'. Defaults to `non-zero'. */
@property(copy) NSString *fillRule;
/* The color to fill the path's stroked outline, or nil for no stroking.
* Defaults to nil. Animatable. */
@property(nullable) CGColorRef strokeColor;
/* These values define the subregion of the path used to draw the
* stroked outline. The values must be in the range [0,1] with zero
* representing the start of the path and one the end. Values in
* between zero and one are interpolated linearly along the path
* length. strokeStart defaults to zero and strokeEnd to one. Both are
* animatable. */
@property CGFloat strokeStart;
@property CGFloat strokeEnd;
/* The line width used when stroking the path. Defaults to one.
* Animatable. */
@property CGFloat lineWidth;
/* The miter limit used when stroking the path. Defaults to ten.
* Animatable. */
@property CGFloat miterLimit;
/* The cap style used when stroking the path. Options are `butt', `round'
* and `square'. Defaults to `butt'. */
@property(copy) NSString *lineCap;
/* The join style used when stroking the path. Options are `miter', `round'
* and `bevel'. Defaults to `miter'. */
@property(copy) NSString *lineJoin;
/* The phase of the dashing pattern applied when creating the stroke.
* Defaults to zero. Animatable. */
@property CGFloat lineDashPhase;
/* The dash pattern (an array of NSNumbers) applied when creating the
* stroked version of the path. Defaults to nil. */
@property(nullable, copy) NSArray<NSNumber *> *lineDashPattern;
@end
/* `fillRule' values. */
CA_EXTERN NSString *const kCAFillRuleNonZero
CA_AVAILABLE_STARTING (10.6, 3.0, 9.0, 2.0);
CA_EXTERN NSString *const kCAFillRuleEvenOdd
CA_AVAILABLE_STARTING (10.6, 3.0, 9.0, 2.0);
/* `lineJoin' values. */
CA_EXTERN NSString *const kCALineJoinMiter
CA_AVAILABLE_STARTING (10.6, 3.0, 9.0, 2.0);
CA_EXTERN NSString *const kCALineJoinRound
CA_AVAILABLE_STARTING (10.6, 3.0, 9.0, 2.0);
CA_EXTERN NSString *const kCALineJoinBevel
CA_AVAILABLE_STARTING (10.6, 3.0, 9.0, 2.0);
/* `lineCap' values. */
CA_EXTERN NSString *const kCALineCapButt
CA_AVAILABLE_STARTING (10.6, 3.0, 9.0, 2.0);
CA_EXTERN NSString *const kCALineCapRound
CA_AVAILABLE_STARTING (10.6, 3.0, 9.0, 2.0);
CA_EXTERN NSString *const kCALineCapSquare
CA_AVAILABLE_STARTING (10.6, 3.0, 9.0, 2.0);
NS_ASSUME_NONNULL_END
Overview
在其坐標(biāo)空間中繪制三次Bezier樣條曲線的圖層。

shape是在圖層內(nèi)容和第一個(gè)子圖層之間合成的。
shape將被繪制反鋸齒,并且在可能的情況下將被映射到屏幕空間,然后進(jìn)行光柵化以保持分辨率的獨(dú)立性。 但是,應(yīng)用于圖層或其父類的某些圖像處理操作(如CoreImage濾鏡)可能會(huì)強(qiáng)制在本地坐標(biāo)空間中進(jìn)行光柵化。
注意:與精度相比,Shape光柵化可能更有利于速度。 例如,具有多個(gè)相交路徑段的像素可能不會(huì)給出確切的結(jié)果。
shape layer在其坐標(biāo)空間中繪制一個(gè)三次貝塞爾樣條曲線。
樣條曲線使用CGPath對(duì)象進(jìn)行描述,并可能同時(shí)填充和描邊組件(在這種情況下描邊被合成填充)。 整個(gè)形狀是在圖層之間合成的內(nèi)容及其第一個(gè)子層。
路徑對(duì)象可以使用任何具體的CAPropertyAnimation的子類來動(dòng)畫。 路徑將作為線性混合插值"on-line"點(diǎn); 可以非線性地插入"off-line"點(diǎn)(例如保持曲線衍生物的連續(xù)性)。 如果兩個(gè)路徑有不同的控制數(shù)量點(diǎn)或段的結(jié)果是不確定的。
Topics
1. Specifying the Shape Path
-
path
- 定義要呈現(xiàn)的形狀的路徑,可動(dòng)畫。
2. Accessing Shape Style Properties
-
- 用于填充形狀路徑的顏色,可動(dòng)畫。
-
- 填充形狀路徑時(shí)使用的填充規(guī)則。
-
- 指定形狀路徑的線條樣式。
-
- 描邊時(shí),虛線圖案應(yīng)用于形狀的路徑。
-
- 描邊時(shí),應(yīng)用于形狀的路徑描邊階段,可動(dòng)畫。
-
- 指定形狀路徑的線條連接樣式。
-
- 指定形狀路徑的線寬,可動(dòng)畫。
-
- 描邊形狀路徑時(shí)使用的尖角限制,可動(dòng)畫。
-
- 用于描繪形狀路徑的顏色,可動(dòng)畫。
-
- 開始描邊路徑的相對(duì)位置,可動(dòng)畫。
-
- 停止描邊路徑的相對(duì)位置,可動(dòng)畫。
3. Constants
-
- 這些常量指定fillRule的可能填充模式。
-
- 這些常量指定描邊路徑的連接段之間的連接點(diǎn)的形狀。 圖lineJoin. Figure 1顯示了線條連接樣式的外觀。

-
Line Cap Values
- 這些常量指定描邊時(shí)開放路徑的端點(diǎn)形狀。 圖 lineCap.Figure 1顯示了線帽樣式的外觀。

后記
下一篇將主要介紹CAShapeLayer的基本使用。
