iOS--坐標(biāo)變換Quartz 2D中的CGContextTranslateCTM、CGContextScaleCTM、

在iOS中,Quartz 2D提供了坐標(biāo)變換支持。

一、特殊的坐標(biāo)變換(平移、縮放、旋轉(zhuǎn))

1. - void CGContextTranslateCTM ( CGContextRef c, CGFloat tx, CGFloat ty ):平移坐標(biāo)系統(tǒng)。

該方法相當(dāng)于把原來(lái)位于 (0, 0) 位置的坐標(biāo)原點(diǎn)平移到 (tx, ty) 點(diǎn)。在平移后的坐標(biāo)系統(tǒng)上繪制圖形時(shí),所有坐標(biāo)點(diǎn)的 X 坐標(biāo)都相當(dāng)于增加了 tx,所有點(diǎn)的 Y 坐標(biāo)都相當(dāng)于增加了 ty。

2. - void CGContextScaleCTM ( CGContextRef c, CGFloat sx, CGFloat sy ):縮放坐標(biāo)系統(tǒng)。

該方法控制坐標(biāo)系統(tǒng)水平方向上縮放 sx,垂直方向上縮放 sy。在縮放后的坐標(biāo)系統(tǒng)上繪制圖形時(shí),所有點(diǎn)的 X 坐標(biāo)都相當(dāng)于乘以 sx 因子,所有點(diǎn)的 Y 坐標(biāo)都相當(dāng)于乘以 sy 因子。

3. - void CGContextRotateCTM ( CGContextRef c, CGFloat angle ):旋轉(zhuǎn)坐標(biāo)系統(tǒng)。

該方法控制坐標(biāo)系統(tǒng)旋轉(zhuǎn) angle 弧度。在縮放后的坐標(biāo)系統(tǒng)上繪制圖形時(shí),所有坐標(biāo)點(diǎn)的 X、Y 坐標(biāo)都相當(dāng)于旋轉(zhuǎn)了 angle弧度之后的坐標(biāo)。

為了讓開(kāi)發(fā)者在進(jìn)行坐標(biāo)變換時(shí)無(wú)須計(jì)算多次坐標(biāo)變換后的累加結(jié)果,Quartz 2D還提供了如下兩個(gè)方法來(lái)保存、恢復(fù)繪圖狀態(tài)。

- void CGContextSaveGState ( CGContextRef c ):保存當(dāng)前的繪圖狀態(tài)。
- void CGContextRestoreGState ( CGContextRef c ):恢復(fù)之前保存的繪圖狀態(tài)。

需要說(shuō)明的是,CGContextSaveGState() 函數(shù)保存的繪圖狀態(tài),不僅包括當(dāng)前坐標(biāo)系統(tǒng)的狀態(tài),也包括當(dāng)前設(shè)置的填充風(fēng)格、線(xiàn)條風(fēng)格、陰影風(fēng)格等各種繪圖狀態(tài)。但 CGContextSaveGState() 函數(shù)不會(huì)保存當(dāng)前繪制的圖形。

二、通用的坐標(biāo)變換(通過(guò)變換矩陣進(jìn)行變換)

除了以上3個(gè)坐標(biāo)轉(zhuǎn)換方法之外,Quartz 2D提供更通用的坐標(biāo)轉(zhuǎn)換方法。

1、void CGContextConcatCTM ( CGContextRef c, CGAffineTransform transform ):使用 transform 變換矩陣對(duì) CGContextRef 的坐標(biāo)系統(tǒng)執(zhí)行變換,通過(guò)使用坐標(biāo)矩陣可以對(duì)坐標(biāo)系統(tǒng)執(zhí)行任意變換。
2、CGAffineTransform CGContextGetCTM ( CGContextRef c ):獲取CGContextRef的坐標(biāo)系統(tǒng)的變換矩陣。
上述兩個(gè)方法中都涉及一個(gè)關(guān)于矩陣的API:CGAffineTransform。

創(chuàng)建CGAffineTransform的4種方式:

  1. CGAffineTransform CGAffineTransformMakeTranslation ( CGFloat tx, CGFloat ty ):創(chuàng)建進(jìn)行位移變換的變換矩陣。該函數(shù)的兩個(gè)參數(shù)與前面介紹的位移變換的兩個(gè)參數(shù)的作用相同。

  2. CGAffineTransform CGAffineTransformMakeScale ( CGFloat sx, CGFloat sy ):創(chuàng)建進(jìn)行縮放變換的變換矩陣。該函數(shù)的兩個(gè)參數(shù)與前面介紹的縮放變換的兩個(gè)參數(shù)的作用相同。

  3. CGAffineTransform CGAffineTransformMakeRotation ( CGFloat angle ):創(chuàng)建進(jìn)行旋轉(zhuǎn)變換的變換矩陣。該函數(shù)的參數(shù)與前面介紹的旋轉(zhuǎn)變換的參數(shù)的作用相同。

  4. CGAffineTransform CGAffineTransformMake ( CGFloat a, CGFloat b, CGFloat c, CGFloat d, CGFloat tx, CGFloat ty ):該函數(shù)使用自定義變換矩陣執(zhí)行變換。

其中 ( a, b, c, d )會(huì)形成變換矩陣,tx、ty為橫向和縱向的位移,變換后結(jié)果應(yīng)該為:

a, b

(x, y) x (c, d) + (tx, ty) = (xa x yc, xb x yd) + (tx, ty) =(xa x yc + , xb x yd + ty)。

一般坐標(biāo)變換矩陣

1)水平鏡像(繞 Y 軸做對(duì)稱(chēng)變化):CGAffineTransformMake ( -1, 0, 0, 1, 0, 0 )

-1, 0

0, 1

  1. 垂直鏡像(繞 X 軸做對(duì)稱(chēng)變化):CGAffineTransformMake ( 1, 0, 0, -1, 0, 0 )

1, 0

0, -1

3)繞 x=y(tǒng)軸做對(duì)稱(chēng)變化:CGAffineTransformMake ( 0, 1, 1, 0, 0, 0 )

0, 1

1, 0

4)繞x =-y軸做對(duì)稱(chēng)變化:CGAffineTransformMake ( 0, -1, -1, 0, 0, 0 )

0, -1

-1, 0

5)做“水平傾斜”的變換,此時(shí) Y 坐標(biāo)無(wú)須變換,只要將 X 坐標(biāo)增加 tan( angle ) x X 即可(angle 代表順時(shí)針轉(zhuǎn)過(guò)的角度):CGAffineTransformMake ( 1, 0, tan(angle), 1, 0, 0 )

1, 0

tan(angle), 1

6)做“垂直傾斜”的變換,此時(shí) X 坐標(biāo)無(wú)須變換,只要將 Y 坐標(biāo)減少 tan( angle ) x X 即可:CGAffineTransformMake ( 1, -tan(angle), 0, 1 )

1, -tan(angle)

0, 1

三、復(fù)合矩陣坐標(biāo)變換

  1. CGAffineTransform CGAffineTransformTranslate ( CGAffineTransform t, CGFloat tx, CGFloat, ty ):對(duì)已有的變換矩陣 t 額外增加位移變換。

  2. CGAffineTransform CGAffineTransformScale ( CGAffineTransform t, CGFloat sx, CGFloat sy ):對(duì)已有的變換矩陣 t 額外增加縮放變換。

  3. CGAffineTransform CGAffineTransformRotate ( CGAffineTransform t, CGFloat angle ):對(duì)已有的變換矩陣 t 額外增加旋轉(zhuǎn)變換。

  4. CGAffineTransform CGAffineTransformInvert ( CGAffineTransform t ):對(duì)已有的變換矩陣 t 進(jìn)行反轉(zhuǎn)。

  5. CGAffineTransform CGAffineTransformConcat ( CGAffineTransform t1, CGAffineTransform t2 ):將兩個(gè)變換矩陣進(jìn)行疊加。

四、對(duì)CGPoint、CGSize和CGRect進(jìn)行坐標(biāo)變換

  1. CGPoint CGPointApplyAffineTransform ( CGPoint point, CGAffineTransform t ):對(duì)指定的CGPoint執(zhí)行變換,函數(shù)返回坐標(biāo)變換后的CGPoint。

  2. CGSize CGSizeApplyAffineTransform ( CGSize size, CGAffineTransform t ):對(duì)指定的CGSize執(zhí)行變換,函數(shù)返回坐標(biāo)變換后的CGSize。

  3. CGRect CGRectApplyAffineTransform ( CGRect rect, CGAffineTransform t ):對(duì)指定的CGRect執(zhí)行變換,函數(shù)返回坐標(biāo)變換后的CGRect。

五、對(duì)UIView進(jìn)行坐標(biāo)變換

UIView提供了一個(gè)transform屬性,該屬性支持設(shè)置CGAffineTransform變換矩陣,這樣就可對(duì)UIView控件本身進(jìn)行坐標(biāo)變換,從而改變UIView控件本身的外觀。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容