版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2018.09.05 |
前言
Quartz 2D
框架相信大家都知道,也都一直在使用。Quartz 2D
的API是純C語言的,它是一個二維繪圖引擎,同時支持iOS和Mac系統。Quartz 2D
的API來自于Core Graphics
框架,數據類型和函數基本都以CG作為前綴,接下來幾篇我們就一起來看一下這個框架。感興趣可以看上面幾篇文章。
1. Quartz 2D編程指南 (一) —— 簡介(一)
2. Quartz 2D編程指南 (二) —— Quartz 2D概覽(二)
3. Quartz 2D編程指南 (三) —— 圖形上下文(三)
4. Quartz 2D編程指南 (四) —— Paths路徑(一)
5. Quartz 2D編程指南 (五) —— Paths路徑(二)
6. Quartz 2D編程指南 (六) —— 顏色和顏色空間(一)
7. Quartz 2D編程指南 (七) —— 變換(一)
Patterns - 圖案樣式
pattern
是一系列繪制操作,重復繪制到圖形上下文。 您可以使用與使用顏色相同的方式使用模式。 當您使用pattern
進行繪制時,Quartz將頁面劃分為一組pattern
單元格,每個單元格都是圖案pattern
圖像的大小,并使用您提供的回調繪制每個單元格。 圖6-1顯示了繪制到窗口圖形上下文的pattern
。
The Anatomy of a Pattern - 圖案的剖析
模式pattern
單元是模式的基本組件。 圖6-1中所示模式的模式單元如圖6-2所示。 黑色矩形不是圖案的一部分,它被繪制以顯示模式單元格的結束位置。
該特定圖案單元的大小包括四個彩色矩形的區域以及矩形上方和右側的空間,如圖6-3所示。 圖中每個圖案單元周圍的黑色矩形不是單元的一部分; 它被繪制以指示單元的邊界。 創建模式單元格時,可以定義單元格的邊界并在邊界內繪制。
您可以指定Quartz在水平和垂直方向上繪制每個圖案單元的起點與下一個圖案單元的距離。 繪制圖6-3中的圖案單元,使得一個圖案單元的開始正好是與下一個圖案單元分開的圖案寬度,導致每個圖案單元在下一個圖案單元上鄰接。 圖6-4中的圖案單元在水平和垂直兩個方向上都添加了空間。 您可以為每個方向指定不同的間距值。 如果使間距小于圖案單元格的寬度或高度,則圖案單元格會重疊。
繪制圖案單元格時,Quartz使用模式空間(pattern space)
作為坐標系。(pattern space)
是一個抽象空間,它通過您在創建模式時指定的變換矩陣(模式矩陣)映射到默認用戶空間。
注意:模式空間
(pattern space)
與用戶空間分開。無論當前轉換矩陣的狀態如何,未轉換的模式空間都映射到基本(未轉換的)用戶空間。將變換應用于模式空間時,Quartz僅將變換應用于模式空間。模式坐標系的默認約定是底層圖形上下文的約定。默認情況下,Quartz使用坐標系,其中正x值表示向右的位移,正y值表示向上位移。但是,UIKit創建的圖形上下文使用不同的約定,其中正y值表示向下位移。雖然此約定通常通過將變換連接到坐標系統來應用于圖形上下文,但在這種情況下,Quartz還會修改模式空間的默認約定以進行匹配。
如果您不希望Quartz轉換模式單元格,則可以指定單位矩陣。但是,您可以通過提供轉換矩陣來實現有趣的效果。圖6-5顯示了縮放圖6-2中所示的模式單元的效果。圖6-6演示了旋轉模式單元。轉換模式單元有點微妙。圖6-7顯示了圖案的原點,圖案單元在水平和垂直兩個方向上平移,因此圖案不再像圖6-1那樣鄰接窗口。
Colored Patterns and Stencil (Uncolored) Patterns - 彩色模式和模具(無色)模式
彩色圖案(Colored patterns)
具有與之相關的固有顏色。 更改用于創建模式單元格的顏色,模式失去其含義。 蘇格蘭格子(如圖6-8中所示的樣本)是彩色圖案的一個例子。 彩色圖案中的顏色被指定為圖案單元格創建過程的一部分,而不是圖案繪制過程的一部分。
其他圖案僅根據其形狀定義,因此可以被認為是模板圖案(stencil patterns)
,無色圖案,甚至是圖像蒙版。 圖6-9中顯示的紅色和黑色星是每個相同模式單元的再現。 單元本身由一個形狀 - 一個填充的星星組成。 定義圖案單元格時,沒有顏色與之相關聯。 顏色被指定為圖案繪制過程的一部分,而不是圖像單元創建的一部分。
您可以在Quartz 2D中創建任何一種圖案(colored or stencil)
。
Tiling - 平鋪
平鋪是將圖案單元格(pattern cell)
渲染到頁面的一部分的過程。 當Quartz向設備呈現模式(pattern)
時,Quartz
可能需要調整模式以適應設備空間。 也就是說,由于用戶空間單元和設備像素之間的差異,在用戶空間中定義的模式單元在渲染到設備時可能不完美。
Quartz有三個平鋪選項,可用于在必要時調整模式。 Quartz可以保存:
- 該模式以犧牲圖案單元之間的間距為代價,但不超過一個設備像素。 這被稱為無失真
(no distortion)
。 - 單元之間的間隔,代價是略微扭曲圖案單元,但不超過一個設備像素。 這被稱為具有最小失真的恒定間隔
(constant spacing with minimal distortion)
。 - 單元之間的間隔(對于最小失真選項),代價是扭曲模式單元,以達到快速平鋪所需的程度。 這被稱為恒定間距
(constant spacing)
。
How Patterns Work - Patterns工作原理
圖案的操作與顏色類似,因為您設置了填充或描邊圖案,然后調用繪畫函數。 Quartz使用您設置為“paint”
的模式。例如,如果要繪制帶有純色的填充矩形,則首先調用函數(如CGContextSetFillColor
)來設置填充顏色。然后調用函數CGContextFillRect
以使用指定的顏色繪制填充的矩形。要使用模式繪制,首先調用函數CGContextSetFillPattern
來設置模式。然后調用CGContextFillRect
實際使用指定的模式繪制填充的矩形。繪畫與顏色和圖案之間的區別在于您必須定義pattern
。您將pattern
和顏色信息提供給函數CGContextSetFillPattern
。您將看到如何在Painting Colored Patterns和Painting Stencil Patterns中創建,設置和繪制pattern
。
下面是Quartz如何在幕后工作以使用您提供的模式進行繪制的示例。當您使用模式填充或描邊時,Quartz在概念上執行以下任務來繪制每個模式單元格:
- 保存圖形狀態。
- 將當前變換矩陣轉換為模式單元的原點。
- 將CTM與模式矩陣連接在一起。
- 剪輯到模式單元格的邊界矩形。
- 調用繪圖回調來繪制模式單元格。
- 恢復圖形狀態。
Quartz為您處理所有平鋪,重復將圖案單元格渲染到繪圖空間,直到繪制整個空間。您可以使用圖案填充或描邊。模式單元格可以是您指定的任何大小。如果要查看圖案,則應確保圖案單元格適合繪圖空間。例如,如果您的圖案單元格為8個單位乘以10個單位,并且您使用該圖案來描繪寬度為2個單位的線條,則圖案單元格將被裁剪,因為它是10個單位寬度。在這種情況下,您可能無法識別該模式。
Painting Colored Patterns - 繪畫彩色圖案
以下各節介紹了繪制彩色圖案所需執行的五個步驟:
- 1) Write a Callback Function That Draws a Colored Pattern Cell
- 2) Set Up the Colored Pattern Color Space
- 3) Set Up the Anatomy of the Colored Pattern
- 4) Specify the Colored Pattern as a Fill or Stroke Pattern
- 5) Draw With the Colored Pattern
這些步驟與用于繪制模板圖案的步驟相同。 兩者之間的區別在于您如何設置顏色信息。 您可以在A Complete Colored Pattern Painting Function中查看所有步驟如何組合在一起。
1. Write a Callback Function That Draws a Colored Pattern Cell - 編寫繪制彩色圖案單元格的回調函數
模式單元格看起來怎么樣完全取決于您。 對于此示例,Listing 6-1中的代碼繪制了圖6-2中所示的模式單元。 回想一下,模式單元周圍的黑線不是單元的一部分,它的繪制表明模式單元格的邊界大于代碼繪制的矩形。 您稍后將模式大小指定為Quartz。
您的模式單元格繪制函數是一個遵循此形式的回調:
typedef void (*CGPatternDrawPatternCallback) (
void *info,
CGContextRef context
);
你可以隨意命名你的回調。Listing 6-1中的名為MyDrawColoredPattern
。回調有兩個參數:
-
info
,指向與pattern
關聯的私有數據的通用指針。此參數是可選的;你可以傳遞NULL
。傳遞給回調的數據與您稍后在創建模式時提供的數據相同。 -
context
,用于繪制模式單元格的圖形上下文。
Listing 6-1
中的代碼繪制的模式單元格是任意的。您的代碼會繪制適合您創建的模式的任何內容。有關代碼的這些詳細信息非常重要:
- 聲明了
pattern size
。在編寫繪圖代碼時,需要牢記圖案尺寸。這里,大小被聲明為全局。除評論外,繪圖函數不具體指代大小。稍后,您將圖案大小指定為Quartz 2D
。請參閱Set Up the Anatomy of the Colored Pattern。 - 繪圖函數遵循由
CGPatternDrawPatternCallback
回調類型定義定義的原型。 - 在代碼中執行的繪圖設置顏色,這使其成為彩色圖案。
// Listing 6-1 A drawing callback that draws a colored pattern cell
#define H_PATTERN_SIZE 16
#define V_PATTERN_SIZE 18
void MyDrawColoredPattern (void *info, CGContextRef myContext)
{
CGFloat subunit = 5; // the pattern cell itself is 16 by 18
CGRect myRect1 = {{0,0}, {subunit, subunit}},
myRect2 = {{subunit, subunit}, {subunit, subunit}},
myRect3 = {{0,subunit}, {subunit, subunit}},
myRect4 = {{subunit,0}, {subunit, subunit}};
CGContextSetRGBFillColor (myContext, 0, 0, 1, 0.5);
CGContextFillRect (myContext, myRect1);
CGContextSetRGBFillColor (myContext, 1, 0, 0, 0.5);
CGContextFillRect (myContext, myRect2);
CGContextSetRGBFillColor (myContext, 0, 1, 0, 0.5);
CGContextFillRect (myContext, myRect3);
CGContextSetRGBFillColor (myContext, .5, 0, .5, 0.5);
CGContextFillRect (myContext, myRect4);
}
2. Set Up the Colored Pattern Color Space - 設置彩色圖案顏色空間
Listing 6-1
中的代碼使用顏色繪制圖案單元格。 您必須通過將基本模式顏色空間設置為NULL
來確保Quartz
使用您在繪圖例程中使用的顏色進行繪制,如Listing 6-2
所示。 列表后面的每個編號行代碼有詳細說明。
// Listing 6-2 Creating a base pattern color space
CGColorSpaceRef patternSpace;
patternSpace = CGColorSpaceCreatePattern (NULL);// 1
CGContextSetFillColorSpace (myContext, patternSpace);// 2
CGColorSpaceRelease (patternSpace);// 3
這是代碼的作用:
- 1)通過調用函數
CGColorSpaceCreatePattern
創建適合彩色圖案的圖案顏色空間,將NULL
作為基色空間傳遞。 - 2)將填充顏色空間設置為圖案顏色空間。 如果您正在
stroke
圖案,請調用CGContextSetStrokeColorSpace
。 - 3)釋放圖案顏色空間。
3. Set Up the Anatomy of the Colored Pattern - 建立彩色圖案Anatomy
有關anatomy of a pattern
的信息保存在CGPattern
對象中。 您可以通過調用函數CGPatternCreate
來創建CGPattern
對象,其原型如Listing 6-3
所示。
// Listing 6-3 The CGPatternCreate function prototype
CGPatternRef CGPatternCreate ( void *info,
CGRect bounds,
CGAffineTransform matrix,
CGFloat xStep,
CGFloat yStep,
CGPatternTiling tiling,
bool isColored,
const CGPatternCallbacks *callbacks );
info
參數是指向要傳遞給繪圖回調的數據的指針。這是 Write a Callback Function That Draws a Colored Pattern Cell中討論的相同指針。
您可以在bounds
參數中指定圖案單元格的大小size。matrix
參數指定圖案矩陣,它將圖案坐標系映射到圖形上下文的默認坐標系。如果要使用與圖形上下文相同的坐標系繪制圖案,請使用單位矩陣。 xStep
和yStep
參數指定圖案坐標系中單元格之間的水平和垂直間距。請參閱The Anatomy of a Pattern以查看有關邊界,圖案矩陣和間距的信息。
tiling
參數可以是以下三個值之一:
kCGPatternTilingNoDistortion
kCGPatternTilingConstantSpacingMinimalDistortion
kCGPatternTilingConstantSpacing
請參閱Tiling以查看有關平鋪的信息。
isColored
參數指定圖案單元格是彩色圖案(true)
還是模板圖案(false)
。如果在此處傳遞true,則繪圖圖案回調指定圖案顏色,并且必須將圖案顏色空間設置為彩色圖案顏色空間(請參閱Set Up the Colored Pattern Color Space)。
傳遞給函數CGPatternCreate
的最后一個參數是指向CGPatternCallbacks
數據結構的指針。這個結構有三個字段:
struct CGPatternCallbacks
{
unsigned int version;
CGPatternDrawPatternCallback drawPattern;
CGPatternReleaseInfoCallback releaseInfo;
};
將version
字段設置為0
。drawPattern
字段是指向繪圖回調的指針。 releaseInfo
字段是指向釋放CGPattern
對象時調用的回調的指針,用于釋放傳遞給繪圖回調的info
參數的存儲。 如果未在此參數中傳遞任何數據,則將此字段設置為NULL
。
4. Specify the Colored Pattern as a Fill or Stroke Pattern - 將彩色圖案指定為填充或描邊圖案
您可以通過調用相應的函數CGContextSetFillPattern
或CGContextSetStrokePattern
來使用您的圖案進行填充或描邊。 Quartz使用您的模式進行任何后續填充或描邊。
這些函數各有三個參數:
- 圖形上下文
- 您之前創建的
CGPattern
對象 - 一系列顏色組件
盡管彩色圖案提供了自己的顏色,但您必須傳遞單個Alpha值以通知Quartz
繪制時圖案的整體不透明度。 Alpha可以從1(完全不透明)到0(完全透明)變化。 這些代碼行顯示了如何為用于填充的彩色圖案設置不透明度的示例。
CGFloat alpha = 1;
CGContextSetFillPattern (myContext, myPattern, &alpha);
5. Draw With the Colored Pattern - 用彩色圖案繪制
完成上述步驟后,您可以調用任何繪制的Quartz 2D
函數。 您的圖案用作“paint”
。例如,您可以調用CGContextStrokePath
,CGContextFillPath
,CGContextFillRect
或任何其他繪制函數。
6. A Complete Colored Pattern Painting Function - 一個完整的彩色圖案繪畫函數
Listing 6-4
中的代碼包含一個繪制彩色圖案的函數。 該函數包含了前面討論的所有步驟。 列表后面有每個編號行代碼的詳細說明。
// Listing 6-4 A function that paints a colored pattern
void MyColoredPatternPainting (CGContextRef myContext,
CGRect rect)
{
CGPatternRef pattern;// 1
CGColorSpaceRef patternSpace;// 2
CGFloat alpha = 1,// 3
width, height;// 4
static const CGPatternCallbacks callbacks = {0, // 5
&MyDrawPattern,
NULL};
CGContextSaveGState (myContext);
patternSpace = CGColorSpaceCreatePattern (NULL);// 6
CGContextSetFillColorSpace (myContext, patternSpace);// 7
CGColorSpaceRelease (patternSpace);// 8
pattern = CGPatternCreate (NULL, // 9
CGRectMake (0, 0, H_PSIZE, V_PSIZE),// 10
CGAffineTransformMake (1, 0, 0, 1, 0, 0),// 11
H_PATTERN_SIZE, // 12
V_PATTERN_SIZE, // 13
kCGPatternTilingConstantSpacing,// 14
true, // 15
&callbacks);// 16
CGContextSetFillPattern (myContext, pattern, &alpha);// 17
CGPatternRelease (pattern);// 18
CGContextFillRect (myContext, rect);// 19
CGContextRestoreGState (myContext);
}
這是代碼的作用:
- 1)聲明稍后創建的
CGPattern
對象的存儲。 - 2)聲明稍后創建的圖像顏色空間的存儲。
- 3)聲明alpha的變量并將其設置為1,它將模式的不透明度指定為完全不透明。
- 4)聲明變量以保持窗口的高度和寬度。在此示例中,圖案繪制在窗口區域上。
- 5)聲明并填充回調結構,將
0
作為版本傳遞,并將指針傳遞給繪圖回調函數。此示例未提供釋放信息回調,因此該字段設置為NULL
。 - 6)創建圖案顏色空間對象,將圖案的基色空間設置為
NULL
。繪制彩色圖案時,圖案在圖形回調中提供自己的顏色,這就是您將顏色空間設置為NULL
的原因。 - 7)將填充顏色空間設置為剛剛創建的圖案顏色空間對象。
- 8)釋放圖案顏色空間對象。
- 9)傳遞
NULL
,因為圖案不需要傳遞給繪圖回調的任何其他信息。 - 10)傳遞一個
CGRect
對象,該對象指定圖案單元格的邊界。 - 11)傳遞
CGAffineTransform
矩陣,該矩陣指定如何將圖案空間轉換為使用圖案的上下文的默認用戶空間。此示例傳遞單位矩陣。 - 12)將水平圖案大小作為每個單元格開頭之間的水平位移。在此示例中,一個單元格與下一個單元格相鄰。
- 13)將垂直圖案大小作為每個單元格開始之間的垂直位移。
- 14)傳遞常量
kCGPatternTilingConstantSpacing
以指定Quartz
應如何呈現圖案。有關更多信息,請參閱Tiling。 - 15)對
isColored
參數傳遞true,以指定圖案為彩色圖案。 - 16)傳遞指向包含版本信息的回調結構的指針,以及指向繪圖回調函數的指針。
- 17)設置填充模式,傳遞上下文,剛剛創建的
CGPattern
對象,以及指向Alpha
值的指針,該值指定Quartz應用于模式的不透明度。 - 18)釋放
CGPattern
對象。 - 19)填充一個矩形,該矩形是傳遞給
MyColoredPatternPainting
例程的窗口大小。Quartz
使用您剛剛設置的圖案填充矩形。
Painting Stencil Patterns - 繪制模板圖案
以下各節介紹了繪制模板圖案所需執行的五個步驟:
- 1) Write a Callback Function That Draws a Stencil Pattern Cell
- 2) Set Up the Stencil Pattern Color Space
- 3) Set Up the Anatomy of the Stencil Pattern
- 4) Specify the Stencil Pattern as a Fill or Stroke Pattern
- 5) Drawing with the Stencil Pattern
這些實際上與您用于繪制彩色圖案的步驟相同。 兩者之間的區別在于您如何設置顏色信息。 您可以在A Complete Stencil Pattern Painting Function中查看所有步驟如何組合在一起。
1. Write a Callback Function That Draws a Stencil Pattern Cell - 編寫一個繪制模板模式單元的回調函數
您為繪制模板圖案而編寫的回調遵循與彩色圖案單元格所描述的相同的形式。 請參閱 Write a Callback Function That Draws a Colored Pattern Cell。 唯一的區別是您的繪圖回調沒有指定任何顏色。 圖6-10中顯示的圖案單元格沒有從圖形回調中獲得顏色。 顏色設置在圖案顏色空間中的繪圖顏色之外。
看一下Listing 6-5
中的代碼,它繪制了圖6-10中所示的模式單元。 請注意,代碼只是創建一個路徑并填充路徑。 代碼沒有設置顏色。
// Listing 6-5 A drawing callback that draws a stencil pattern cell
#define PSIZE 16 // size of the pattern cell
static void MyDrawStencilStar (void *info, CGContextRef myContext)
{
int k;
double r, theta;
r = 0.8 * PSIZE / 2;
theta = 2 * M_PI * (2.0 / 5.0); // 144 degrees
CGContextTranslateCTM (myContext, PSIZE/2, PSIZE/2);
CGContextMoveToPoint(myContext, 0, r);
for (k = 1; k < 5; k++) {
CGContextAddLineToPoint (myContext,
r * sin(k * theta),
r * cos(k * theta));
}
CGContextClosePath(myContext);
CGContextFillPath(myContext);
}
2. Set Up the Stencil Pattern Color Space - 設置模板圖案顏色空間
模板圖案要求您為Quartz設置圖案顏色空間以進行繪制,如Listing 6-6
所示。 列表后面的每個編號行代碼的詳細說明。
// Listing 6-6 Code that creates a pattern color space for a stencil pattern
CGPatternRef pattern;
CGColorSpaceRef baseSpace;
CGColorSpaceRef patternSpace;
baseSpace = CGColorSpaceCreateWithName (kCGColorSpaceGenericRGB);// 1
patternSpace = CGColorSpaceCreatePattern (baseSpace);// 2
CGContextSetFillColorSpace (myContext, patternSpace);// 3
CGColorSpaceRelease(patternSpace);// 4
CGColorSpaceRelease(baseSpace);// 5
這是代碼的作用:
- 1) 此函數創建一個通用RGB空間。 通用顏色空間使顏色與系統匹配。 有關更多信息,請參閱 Creating Generic Color Spaces。
- 2) 創建圖案顏色空間。 您提供的顏色空間指定了如何為圖案表示顏色。 稍后,當您為圖案設置顏色時,必須使用圖案顏色空間設置它們。 對于此示例,您需要使用RGB值指定顏色。
- 3) 設置填充圖案時要使用的顏色空間。 您可以通過調用
CGContextSetStrokeColorSpace
函數來設置描邊顏色空間。 - 4) 釋放圖案顏色空間對象。
- 5) 釋放基色空間對象。
3. Set Up the Anatomy of the Stencil Pattern - 建立模板圖案的剖析
Stencil patterns
要求您為Quartz
設置圖案顏色空間以進行繪制,如Listing 6-6所示。 列表后面的每個編號行代碼的詳細說明。
// Listing 6-6 Code that creates a pattern color space for a stencil pattern
CGPatternRef pattern;
CGColorSpaceRef baseSpace;
CGColorSpaceRef patternSpace;
baseSpace = CGColorSpaceCreateWithName (kCGColorSpaceGenericRGB);// 1
patternSpace = CGColorSpaceCreatePattern (baseSpace);// 2
CGContextSetFillColorSpace (myContext, patternSpace);// 3
CGColorSpaceRelease(patternSpace);// 4
CGColorSpaceRelease(baseSpace);// 5
這是代碼的作用:
- 1) 此函數創建一個通用RGB空間。 通用顏色空間使顏色與系統匹配。 有關更多信息,請參閱Creating Generic Color Spaces。
- 2) 創建圖案顏色空間。 您提供的顏色空間指定了如何為圖案表示顏色。 稍后,當您為圖案設置顏色時,必須使用圖案顏色空間設置它們。 對于此示例,您需要使用RGB值指定顏色。
- 3) 設置填充圖案時要使用的顏色空間。 您可以通過調用
CGContextSetStrokeColorSpace
函數來設置描邊顏色空間。 - 4) 釋放圖案顏色空間對象。
- 5) 釋放基色空間對象。
4. Specify the Stencil Pattern as a Fill or Stroke Pattern - 將模板圖案指定為填充或描邊圖案
您可以通過調用相應的函數CGContextSetFillPattern
或CGContextSetStrokePattern
來使用您的模式進行填充或描邊。 Quartz
使用您的模式進行任何后續填充或描邊。
這些函數各有三個參數:
- 1) 圖形上下文
- 2) 您之前創建的
CGPattern
對象 - 3) 一系列顏色組件
模板圖案不會在圖形回調中提供顏色,因此您必須將顏色傳遞給填充或描邊函數以通知Quartz要使用的顏色。 Listing 6-7
顯示了如何為模板圖案設置顏色的示例。 顏色數組中的值由Quartz在您之前設置的顏色空間中解釋。 由于此示例使用設備RGB,因此顏色數組包含紅色,綠色和藍色組件的值。 第四個值指定顏色的不透明度。
// Listing 6-7 Code that sets opacity for a colored pattern
static const CGFloat color[4] = { 0, 1, 1, 0.5 }; //cyan, 50% transparent
CGContextSetFillPattern (myContext, myPattern, color);
5. Drawing with the Stencil Pattern - 用模具圖案繪圖
完成上述步驟后,您可以調用任何繪制的Quartz 2D
函數。 您的模式用作“paint”
。例如,您可以調用CGContextStrokePath
,CGContextFillPath
,CGContextFillRect
或任何其他繪制函數。
6. A Complete Stencil Pattern Painting Function - 完整的模板圖案繪畫功能
Listing 6-8
中的代碼包含一個繪制模板圖案的函數。 該功能包含了前面討論的所有步驟。 列表后面列出了每個編號行代碼的詳細說明。
Listing 6-8 A function that paints a stencil pattern
#define PSIZE 16
void MyStencilPatternPainting (CGContextRef myContext,
const Rect *windowRect)
{
CGPatternRef pattern;
CGColorSpaceRef baseSpace;
CGColorSpaceRef patternSpace;
static const CGFloat color[4] = { 0, 1, 0, 1 };// 1
static const CGPatternCallbacks callbacks = {0, &drawStar, NULL};// 2
baseSpace = CGColorSpaceCreateDeviceRGB ();// 3
patternSpace = CGColorSpaceCreatePattern (baseSpace);// 4
CGContextSetFillColorSpace (myContext, patternSpace);// 5
CGColorSpaceRelease (patternSpace);
CGColorSpaceRelease (baseSpace);
pattern = CGPatternCreate(NULL, CGRectMake(0, 0, PSIZE, PSIZE),// 6
CGAffineTransformIdentity, PSIZE, PSIZE,
kCGPatternTilingConstantSpacing,
false, &callbacks);
CGContextSetFillPattern (myContext, pattern, color);// 7
CGPatternRelease (pattern);// 8
CGContextFillRect (myContext,CGRectMake (0,0,PSIZE*20,PSIZE*20));// 9
}
這是代碼的作用:
- 1) 聲明一個數組以保存顏色值并將值(將在RGB顏色空間中)設置為不透明綠色。
- 2) 聲明并填充回調結構,將
0
作為版本傳遞,并將指針傳遞給繪圖回調函數。此示例未提供發布信息回調,因此該字段設置為NULL
。 - 3) 創建RGB設備顏色空間。如果將圖案繪制到顯示器,則需要提供此類型的色彩空間。
- 4) 從RGB設備顏色空間創建圖案顏色空間對象。
- 5) 將填充顏色空間設置為剛剛創建的圖案顏色空間對象。
- 6) 創建一個圖案對象。請注意,倒數第二個參數 -
isColored
參數 - 為false
。模板圖案不提供顏色,因此您必須為此參數傳遞false
。所有其他參數類似于為彩色圖案示例傳遞的參數。請參閱A Complete Colored Pattern Painting Function。 - 7) 設置填充圖案,傳遞先前聲明的顏色數組。
- 8) 釋放
CGPattern
對象。 - 9) 填充矩形。 Quartz使用您剛剛設置的圖案填充矩形。
后記
本篇主要講述了Patterns圖案樣式,感興趣的給個贊或者關注~~~