Quartz2D之Path使用初步

2022-04-03 07:22:19 字數 2077 閱讀 2804

//

// myquartzview.m

// quartztest

//// created by zenny_chen on 12-2-21.

//#import "myquartzview.h"

// quartz2d以及core animation所需要的標頭檔案

#import @implementation myquartzview

- (id)initwithframe:(cgrect)frame

return self;

}// only override drawrect: if you perform custom drawing.

// an empty implementation adversely affects performance during animation.

- (void)drawrect:(cgrect)rect

@end

quartz2d中對path的繪製過程與openvg幾乎一樣。首先是建立path,然後是對path做繪製描述,最後是繪製(paint)path。

以上要注意的是,建立完乙個path控制代碼後,必須呼叫一次cgpathmovetopoint來初始化繪製的初始點。

在呼叫cgpathmovetopoint後,它會終止當前的子path,並重新開啟乙個子path。而乙個子path,其預設行為會構成乙個封閉圖形,比如以下**:

// 建立乙個path控制代碼

cgmutablepathref pathref = cgpathcreatemutable();

// 初始化該path到乙個初始點

cgpathmovetopoint(pathref, &cgaffinetransformidentity, 0.0f, 0.0f);

// 新增一條直線,從初始點到該函式指定的座標點

cgpathaddlinetopoint(pathref, &cgaffinetransformidentity, 50.0f, 100.0f);

cgpathaddlinetopoint(pathref, &cgaffinetransformidentity, 100.0f, 50.0f);

// 關閉該path

cgpathclosesubpath(pathref);

新增了兩條直線,但是最終繪製出來的是乙個封閉的三角形。

如果我們要畫兩條均以(0, 0)為其中乙個端點的線段的話,我們可以這麼做:

// 建立乙個path控制代碼

cgmutablepathref pathref = cgpathcreatemutable();

// 初始化該path到乙個初始點

cgpathmovetopoint(pathref, &cgaffinetransformidentity, 0.0f, 0.0f);

// 新增一條直線,從初始點到該函式指定的座標點

cgpathaddlinetopoint(pathref, &cgaffinetransformidentity, 50.0f, 100.0f);

cgpathmovetopoint(pathref, &cgaffinetransformidentity, 0.0f, 0.0f);

cgpathaddlinetopoint(pathref, &cgaffinetransformidentity, 100.0f, 50.0f);

// 關閉該path

cgpathclosesubpath(pathref);

如果我們要從(0, 0)到(50, 100)畫一條線段,然後再要從(50, 100)到(100, 50)畫一條線段的話,可以這麼做:

cgpathaddlinetopoint(pathref, &cgaffinetransformidentity, 50.0f, 100.0f);

cgpathmovetopoint(pathref, &cgaffinetransformidentity, 50.0f, 100.0f);

cgpathaddlinetopoint(pathref, &cgaffinetransformidentity, 100.0f, 50.0f);

Quartz 2D 簡單使用

想要在uiview內部繪製一下東西,需要自定義uiview,並且實現uiview的 void drawrect cgrect rect方法,為什麼需要實現drawrect 方法才能繪圖到 view 上,是因為在 drawrect 方法中才能取得跟 view 相關聯的圖形上下文,那麼什drawrect...

Quartz2D之漸變使用初步

quartz2d提供了兩種漸變填充方法。第一種是使用quartz自帶的gradient填充方法 第二種是使用自定義的著色器。這裡將先描述如何使用cggradient物件來做漸變填充。drawing code 建立quartz上下文 cgcontextref context uigraphicsget...

Quartz2D之漸變使用初步

quartz2d提供了兩種漸變填充方法。第一種是使用quartz自帶的gradient填充方法 第二種是使用自定義的著色器。這裡將先描述如何使用cggradient物件來做漸變填充。drawing code 建立quartz上下文 cgcontextref context uigraphicsget...