CoreAnimation编程指南(三)几何变换
生活随笔
收集整理的這篇文章主要介紹了
CoreAnimation编程指南(三)几何变换
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
本章介紹圖層的幾何組成部分,及他們之間的相互關(guān),同時介紹如何變換矩陣可以產(chǎn)生復(fù)雜的視覺效果。
一、圖層的坐標(biāo)系
1.圖層的坐標(biāo)系在不同平臺上面具有差異性。在iOS系統(tǒng)中,默認(rèn)的坐標(biāo)系統(tǒng)原點在圖層的中心左上角地方,原點向右和向下為正值。在Mac OS X系統(tǒng)中,默認(rèn)的坐標(biāo)系原點在圖層的中心左下角地方,原點向右和向上為正值。坐標(biāo)系的所有值都是浮點類型。你在任何平臺上面創(chuàng)建的圖層都采用該平臺默認(rèn)的坐標(biāo)系。
2.每個圖層定義并維護(hù)自己的坐標(biāo)系,它里面的全部內(nèi)容都由此相關(guān)的坐標(biāo)系指定位置。該準(zhǔn)則同時適應(yīng)于圖層自己的內(nèi)容和它的任何子圖層。因為任何圖層定義了它自己的坐標(biāo)系,CALayer類提供相應(yīng)的方法用于從一個圖層坐標(biāo)系的點、矩形、大小值轉(zhuǎn)化為另一個圖層坐標(biāo)系相應(yīng)的值。
3.一些基于圖層的屬性使用單元坐標(biāo)空間測量它們的值。單元坐標(biāo)空間指定圖層邊界的相對值,而不是絕對值。單元坐標(biāo)空間給定的x和y的值總是在0.0到1.0之間。指定一個沿X軸的值為0.0的點,得到的是圖層左邊緣的一個點,而指定一個1.0的點,則是圖層右邊緣的一個點。(對 Y軸而言,如果是在iOS系統(tǒng),則0.0對應(yīng)于頂部的點,而1.0則是底部的點,而在Mac OS X系統(tǒng),得到的剛好相反,就如之前提到的坐標(biāo)系不同一樣)。而點(0.5,0.5)則剛好是圖層的中心點。 ? 二、指定圖層的幾何
1.雖然圖層和圖層樹與視圖和視圖的結(jié)構(gòu)在很多方面具有相似性,但是圖層的幾何卻不同,它更加簡單通俗。圖層的所有幾何屬性,包括圖層的矩陣變換,都可以隱式和顯式動畫。 ? 下圖顯示可以在上下文中指定圖層幾何的屬性: ? ? ? 2.圖層的position屬性是一個CGPoint的值,它指定圖層相當(dāng)于它父圖層的位置,該值基于父圖層的坐標(biāo)系。
3.圖層的bounds屬性是一個CGRect的值,指定圖層的大小(bounds.size)和圖層的原點(bounds.origin)。當(dāng)你重寫圖層的重畫方法的時候,bounds的原點可以作為圖形上下文的原點。
4.圖層擁有一個隱式的frame,它是position,bounds,anchorPoint和transform屬性的一部分。設(shè)置新的frame將會相應(yīng)的改變圖層的position和bounds屬性,但是frame本身并沒有被保存。但是設(shè)置新的frame時候,bounds的原點不受干擾,bounds的大小變?yōu)閒rame的大小,即bounds.size=frame.size。圖層的位置被設(shè)置為相對于錨點(anchor point)的適合位置。當(dāng)你設(shè)置frame的值的時候,它的計算方式和position、bounds、和anchorPoint的屬性相關(guān)。
5.圖層的anchorPoint屬性是一個CGPoint值,它指定了一個基于圖層bounds的符合位置坐標(biāo)系的位置。錨點(anchor point)指定了bounds相對于position的值,同時也作為變換時候的支點。錨點使用單元空間坐標(biāo)系表示,(0.0,0.0)點接近圖層的原點,而(1.0,1.0)是原點的對角點。改變圖層的父圖層的變換屬性(如果存在的話)將會影響到anchorPoint的方向,具體變化取決于父圖層坐標(biāo)系的Y軸。
6.當(dāng)你設(shè)置圖層的frame屬性的時候,position會根據(jù)錨點(anchorPoint)相應(yīng)的改變,而當(dāng)你設(shè)置圖層的position屬性的時候,bounds會根據(jù)錨點(anchorPoint)做相應(yīng)的改變。
7.iOS 注意:以下示例描述基于Mac OS X的圖層,它的坐標(biāo)系原點基于左下角。在iOS上面,圖層的坐標(biāo)系原點位于左上角,原點向下和向右為正值。這變化用具體數(shù)值顯示,而不是概念描述。 下圖描述了基于錨點的三個示例值: ??
? anchorPoint默認(rèn)值是(0.5,0.5),位于圖層邊界的中心點(如上圖顯示),B點把a(bǔ)nchorPoint設(shè)置為(0.0,0.5)。最后C點(1.0,0.0)把圖層的position設(shè)置為圖層frame的右下角。該圖適用于Mac OS X的圖層。在iOS系統(tǒng)里面,圖層使用不同的坐標(biāo)系,相應(yīng)的(0.0,0.0)位于左上角,而(1.0,1.0)位于右下角。 ? ?? 8.圖層的frame、bounds、position和anchorPoint關(guān)系如下圖所示: ? ? ? 在該示例中,anchorPoint默認(rèn)值為(0.5,0.5),位于圖層的中心點。圖層的position值為(100.0,100.0),bounds為(0.0,0.0,120,80.0)。通過計算得到圖層的frame為(40.0,60.0,120.0,80.0)。
9.如果你新創(chuàng)建一個圖層,則只有設(shè)置圖層的frame為(40.0,60.0,120.0,80.0),相應(yīng)的position屬性值將會自動設(shè)置為(100.0,100.0),而bounds會自動設(shè)置為(0.0,0.0,120.0,80.0)。 下圖顯示一個圖層具有相同的frame(如上圖),但是在該圖中它的anchorPoint屬性值被設(shè)置為(0.0,0.0),位于圖層的左下角位置。 ? ? 10.圖層的frame值同樣為(40.0,60.0,120.0,80.0),bounds的值不變,但是圖層的position值已經(jīng)改變?yōu)?40.0,60.0)。
11.圖層的幾何外形和Cocoa視圖另外一個不同地方是,你可以設(shè)置圖層的一個邊角的半徑來把圖層顯示為圓角。圖層的cornerRadius屬性指定了重繪圖層內(nèi)容,剪切子圖層,繪制圖層的邊界和陰影的時候時候圓角的半徑。
12.圖層的zPosition屬性值指定了該圖層位于Z軸上面位置,zPosition用于設(shè)置圖層相對于圖層的同級圖層的可視位置。 ? 三、圖層的幾何變換-概述
1.圖層一旦創(chuàng)建,你就可以通過矩陣變換來改變一個圖層的幾何形狀。CATransform3D的數(shù)據(jù)結(jié)構(gòu)定義一個同質(zhì)的三維變換(4×4 CGFloat值的矩陣),用于圖層的旋轉(zhuǎn),縮放,偏移,歪斜和應(yīng)用的透視。
2.圖層的兩個屬性指定了變換矩陣:transform和sublayerTransform屬性。圖層的transform屬性指定的矩陣結(jié)合圖層的anchorPoint屬性作用于圖層和圖層的子圖層上面。圖 3顯示在使用anchorPoint默認(rèn)值(0.5,0.5)的時候旋轉(zhuǎn)和縮放變換如何影響一個圖層。而圖4顯示了同樣的矩陣變換在anchorPoint為(0.0,0.0)的時候如何改變一個圖層。圖層的sublayerTransform屬性指定的矩陣只會影響圖層的子圖層,而不會對圖層本身產(chǎn)生影響。
3.你可以通過以下的任何一個方法改變CATransform3D的數(shù)據(jù)結(jié)構(gòu): (1)使用CATransform3D函數(shù) (2)直接修改數(shù)據(jù)結(jié)構(gòu)的成員 (3)使用鍵-值編碼改變鍵路徑
4.CATransform3DIdentity是單位矩陣,該矩陣沒有縮放、旋轉(zhuǎn)、歪斜、透視。把該矩陣應(yīng)用到圖層上面,會把圖層幾何屬性修改為默認(rèn)值。 ? 四、圖層的集合變換-變換函數(shù)
1.使用變換函數(shù)可以在核心動畫里面在操作矩陣。你可以使用這些函數(shù)(如下表)去創(chuàng)建一個矩陣一般后面用于改變圖層或者它的子圖層的transform和sublayerTransform屬性。變換函數(shù)或者直接操或者返回一個CATransform3D的數(shù)據(jù)結(jié)構(gòu)。這可以讓你能夠構(gòu)建簡單或復(fù)雜的轉(zhuǎn)換,以便重復(fù)使用。
2.CATransform3D 變換函數(shù) :偏移、旋轉(zhuǎn)和縮放 (1)CATransform3DMakeTranslation ? Returns a transform that translates by ‘(tx, ty, tz)’. t’ = [1 0 0 0; 0 1 0 0; 0 0 1 0; tx ty tz 1]. (2)CATransform3DTranslate ? Translate ‘t’ by ‘(tx, ty, tz)’ and return the result: * t’ = translate(tx, ty, tz) * t. (3)CATransform3DMakeScale ? Returns a transform that scales by `(sx, sy, sz)’: * t’ = [sx 0 0 0; 0 sy 0 0; 0 0 sz 0; 0 0 0 1]. (4)CATransform3DScale ? Scale ‘t’ by ‘(sx, sy, sz)’ and return the result: * t’ = scale(sx, sy, sz) * t. (5)CATransform3DMakeRotation ? Returns a transform that rotates by ‘a(chǎn)ngle’ radians about the vector ‘(x, y, z)’. If the vector has length zero the identity transform is returned. (6)CATransform3DRotate ? Rotate ‘t’ by ‘a(chǎn)ngle’ radians about the vector ‘(x, y, z)’ and return the result. t’ = rotation(angle, x, y, z) * t. ? 3.旋轉(zhuǎn)的單位采用弧度(radians),而不是角度(degress)。以下兩個函數(shù),你可以在弧度和角度之間切換。 ? CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}; ? CGFloat RadiansToDegrees(CGFloat radians) {return radians * 180 / M_PI;}; ?? 4.核心動畫提供了用于轉(zhuǎn)換矩陣的變換函數(shù)CATransform3DInvert。一般是用反轉(zhuǎn)點內(nèi)轉(zhuǎn)化對象提供反向轉(zhuǎn)換。當(dāng)你需要恢復(fù)一個已經(jīng)被變換了的矩陣的時候,反轉(zhuǎn)將會非常有幫助。反轉(zhuǎn)矩陣乘以逆矩陣值,結(jié)果是原始值。
5.變換函數(shù)同時允許你把CATransform3D矩陣轉(zhuǎn)化為CGAffineTransform(仿射)矩陣,前提是CATransform3D矩陣采用如下表示方法。
6.CATransform3D 與 CGAffineTransform 轉(zhuǎn)換 (1)CATransform3DMakeAffineTransform ? Returns a CATransform3D with the same effect as the passed affine transform. (2)CATransform3DIsAffine ? Returns YES if the passedCATransform3D can be exactly represented an affine transform. (3)CATransform3DGetAffineTransform ? Returns the affine transform represented by the passedCATransform3D. ? 7.變換函數(shù)同時提供了可以比較一個變換矩陣是否是單位矩陣,或者兩個矩陣是否相等。
8.CATransform3D 相等測試 (1)CATransform3DIsIdentity ? Returns YES if the transform is the identity transform. (2)CATransform3DEqualToTransform ? Returns YES if the two transforms are exactly equal.. ? 五、圖層的集合變換-修改變換的數(shù)據(jù)結(jié)構(gòu)
1.你可以修改CATransform3D的數(shù)據(jù)結(jié)構(gòu)的元素為任何其他你想要的數(shù)據(jù)值。下面代碼包含了CATransform3D數(shù)據(jù)結(jié)構(gòu)的定義,結(jié)構(gòu)的成員都在其相應(yīng)的矩陣位置。 ? struct CATransform3D ? { ? ? CGFloat m11, m12, m13, m14; ? ? CGFloat m21, m22, m23, m24; ? ? CGFloat m31, m32, m33, m34; ? ? CGFloat m41, m42, m43, m44; ? }; ? typedef struct CATransform3D CATransform3D; ?? 2.下面代碼中的示例說明了如何配置一個CATransform3D一個角度變換 ? CATransform3D aTransform = CATransform3DIdentity; ? //the value of zDistance affects the sharpness of the transform. ? zDistance = 850; ? aTransform.m34 = 1.0 / -zDistance; ?? 六、圖層的集合變換-通過鍵值路徑修改變換
1.核心動畫擴(kuò)展了鍵-值編碼協(xié)議,允許通過關(guān)鍵路徑獲取和設(shè)置一個圖層的CATransform3D矩陣的值。下面描述了圖層的transform和sublayerTransform屬性的相應(yīng)關(guān)鍵路徑。 (1)rotation.x ? The rotation, in radians, in the x axis. (2)rotation.y ? The rotation, in radians, in the y axis. (3)rotation.z ? The rotation, in radians, in the z axis. (4)rotation ? The rotation, in radians, in the z axis. This is identical to setting the rotation.z field. (5)scale.x ? Scale factor for the x axis. (6)scale.y ? Scale factor for the y axis. (7)scale.z ? Scale factor for the z axis. (8)scale ? Average of all three scale factors. (9)translation.x ? Translate in the x axis. (10)translation.y ? Translate in the y axis. (11)translation.z ? Translate in the z axis. (12)translation ? Translate in the x and y axis. Value is an NSSize or CGSize.
2.你不可以通過Objective-C 2.0的屬性來設(shè)置結(jié)構(gòu)域的值,比如下面的代碼將會無法正常運行: ? myLayer.transform.rotation.x=0; ? 3.替換的辦法是,你必須通過setValue:forKeyPath:或者valueForKeyPath:方法,具體如下: ? [myLayer setValue:[NSNumber numberWithInt:0] forKeyPath:@"transform.rotation.x"];
轉(zhuǎn)自夢維:http://www.dreamingwish.com/dream-2012/coreanimation-programming-guide-c-the-geometric-transformation.html
一、圖層的坐標(biāo)系
1.圖層的坐標(biāo)系在不同平臺上面具有差異性。在iOS系統(tǒng)中,默認(rèn)的坐標(biāo)系統(tǒng)原點在圖層的中心左上角地方,原點向右和向下為正值。在Mac OS X系統(tǒng)中,默認(rèn)的坐標(biāo)系原點在圖層的中心左下角地方,原點向右和向上為正值。坐標(biāo)系的所有值都是浮點類型。你在任何平臺上面創(chuàng)建的圖層都采用該平臺默認(rèn)的坐標(biāo)系。
2.每個圖層定義并維護(hù)自己的坐標(biāo)系,它里面的全部內(nèi)容都由此相關(guān)的坐標(biāo)系指定位置。該準(zhǔn)則同時適應(yīng)于圖層自己的內(nèi)容和它的任何子圖層。因為任何圖層定義了它自己的坐標(biāo)系,CALayer類提供相應(yīng)的方法用于從一個圖層坐標(biāo)系的點、矩形、大小值轉(zhuǎn)化為另一個圖層坐標(biāo)系相應(yīng)的值。
3.一些基于圖層的屬性使用單元坐標(biāo)空間測量它們的值。單元坐標(biāo)空間指定圖層邊界的相對值,而不是絕對值。單元坐標(biāo)空間給定的x和y的值總是在0.0到1.0之間。指定一個沿X軸的值為0.0的點,得到的是圖層左邊緣的一個點,而指定一個1.0的點,則是圖層右邊緣的一個點。(對 Y軸而言,如果是在iOS系統(tǒng),則0.0對應(yīng)于頂部的點,而1.0則是底部的點,而在Mac OS X系統(tǒng),得到的剛好相反,就如之前提到的坐標(biāo)系不同一樣)。而點(0.5,0.5)則剛好是圖層的中心點。 ? 二、指定圖層的幾何
1.雖然圖層和圖層樹與視圖和視圖的結(jié)構(gòu)在很多方面具有相似性,但是圖層的幾何卻不同,它更加簡單通俗。圖層的所有幾何屬性,包括圖層的矩陣變換,都可以隱式和顯式動畫。 ? 下圖顯示可以在上下文中指定圖層幾何的屬性: ? ? ? 2.圖層的position屬性是一個CGPoint的值,它指定圖層相當(dāng)于它父圖層的位置,該值基于父圖層的坐標(biāo)系。
3.圖層的bounds屬性是一個CGRect的值,指定圖層的大小(bounds.size)和圖層的原點(bounds.origin)。當(dāng)你重寫圖層的重畫方法的時候,bounds的原點可以作為圖形上下文的原點。
4.圖層擁有一個隱式的frame,它是position,bounds,anchorPoint和transform屬性的一部分。設(shè)置新的frame將會相應(yīng)的改變圖層的position和bounds屬性,但是frame本身并沒有被保存。但是設(shè)置新的frame時候,bounds的原點不受干擾,bounds的大小變?yōu)閒rame的大小,即bounds.size=frame.size。圖層的位置被設(shè)置為相對于錨點(anchor point)的適合位置。當(dāng)你設(shè)置frame的值的時候,它的計算方式和position、bounds、和anchorPoint的屬性相關(guān)。
5.圖層的anchorPoint屬性是一個CGPoint值,它指定了一個基于圖層bounds的符合位置坐標(biāo)系的位置。錨點(anchor point)指定了bounds相對于position的值,同時也作為變換時候的支點。錨點使用單元空間坐標(biāo)系表示,(0.0,0.0)點接近圖層的原點,而(1.0,1.0)是原點的對角點。改變圖層的父圖層的變換屬性(如果存在的話)將會影響到anchorPoint的方向,具體變化取決于父圖層坐標(biāo)系的Y軸。
6.當(dāng)你設(shè)置圖層的frame屬性的時候,position會根據(jù)錨點(anchorPoint)相應(yīng)的改變,而當(dāng)你設(shè)置圖層的position屬性的時候,bounds會根據(jù)錨點(anchorPoint)做相應(yīng)的改變。
7.iOS 注意:以下示例描述基于Mac OS X的圖層,它的坐標(biāo)系原點基于左下角。在iOS上面,圖層的坐標(biāo)系原點位于左上角,原點向下和向右為正值。這變化用具體數(shù)值顯示,而不是概念描述。 下圖描述了基于錨點的三個示例值: ??
? anchorPoint默認(rèn)值是(0.5,0.5),位于圖層邊界的中心點(如上圖顯示),B點把a(bǔ)nchorPoint設(shè)置為(0.0,0.5)。最后C點(1.0,0.0)把圖層的position設(shè)置為圖層frame的右下角。該圖適用于Mac OS X的圖層。在iOS系統(tǒng)里面,圖層使用不同的坐標(biāo)系,相應(yīng)的(0.0,0.0)位于左上角,而(1.0,1.0)位于右下角。 ? ?? 8.圖層的frame、bounds、position和anchorPoint關(guān)系如下圖所示: ? ? ? 在該示例中,anchorPoint默認(rèn)值為(0.5,0.5),位于圖層的中心點。圖層的position值為(100.0,100.0),bounds為(0.0,0.0,120,80.0)。通過計算得到圖層的frame為(40.0,60.0,120.0,80.0)。
9.如果你新創(chuàng)建一個圖層,則只有設(shè)置圖層的frame為(40.0,60.0,120.0,80.0),相應(yīng)的position屬性值將會自動設(shè)置為(100.0,100.0),而bounds會自動設(shè)置為(0.0,0.0,120.0,80.0)。 下圖顯示一個圖層具有相同的frame(如上圖),但是在該圖中它的anchorPoint屬性值被設(shè)置為(0.0,0.0),位于圖層的左下角位置。 ? ? 10.圖層的frame值同樣為(40.0,60.0,120.0,80.0),bounds的值不變,但是圖層的position值已經(jīng)改變?yōu)?40.0,60.0)。
11.圖層的幾何外形和Cocoa視圖另外一個不同地方是,你可以設(shè)置圖層的一個邊角的半徑來把圖層顯示為圓角。圖層的cornerRadius屬性指定了重繪圖層內(nèi)容,剪切子圖層,繪制圖層的邊界和陰影的時候時候圓角的半徑。
12.圖層的zPosition屬性值指定了該圖層位于Z軸上面位置,zPosition用于設(shè)置圖層相對于圖層的同級圖層的可視位置。 ? 三、圖層的幾何變換-概述
1.圖層一旦創(chuàng)建,你就可以通過矩陣變換來改變一個圖層的幾何形狀。CATransform3D的數(shù)據(jù)結(jié)構(gòu)定義一個同質(zhì)的三維變換(4×4 CGFloat值的矩陣),用于圖層的旋轉(zhuǎn),縮放,偏移,歪斜和應(yīng)用的透視。
2.圖層的兩個屬性指定了變換矩陣:transform和sublayerTransform屬性。圖層的transform屬性指定的矩陣結(jié)合圖層的anchorPoint屬性作用于圖層和圖層的子圖層上面。圖 3顯示在使用anchorPoint默認(rèn)值(0.5,0.5)的時候旋轉(zhuǎn)和縮放變換如何影響一個圖層。而圖4顯示了同樣的矩陣變換在anchorPoint為(0.0,0.0)的時候如何改變一個圖層。圖層的sublayerTransform屬性指定的矩陣只會影響圖層的子圖層,而不會對圖層本身產(chǎn)生影響。
3.你可以通過以下的任何一個方法改變CATransform3D的數(shù)據(jù)結(jié)構(gòu): (1)使用CATransform3D函數(shù) (2)直接修改數(shù)據(jù)結(jié)構(gòu)的成員 (3)使用鍵-值編碼改變鍵路徑
4.CATransform3DIdentity是單位矩陣,該矩陣沒有縮放、旋轉(zhuǎn)、歪斜、透視。把該矩陣應(yīng)用到圖層上面,會把圖層幾何屬性修改為默認(rèn)值。 ? 四、圖層的集合變換-變換函數(shù)
1.使用變換函數(shù)可以在核心動畫里面在操作矩陣。你可以使用這些函數(shù)(如下表)去創(chuàng)建一個矩陣一般后面用于改變圖層或者它的子圖層的transform和sublayerTransform屬性。變換函數(shù)或者直接操或者返回一個CATransform3D的數(shù)據(jù)結(jié)構(gòu)。這可以讓你能夠構(gòu)建簡單或復(fù)雜的轉(zhuǎn)換,以便重復(fù)使用。
2.CATransform3D 變換函數(shù) :偏移、旋轉(zhuǎn)和縮放 (1)CATransform3DMakeTranslation ? Returns a transform that translates by ‘(tx, ty, tz)’. t’ = [1 0 0 0; 0 1 0 0; 0 0 1 0; tx ty tz 1]. (2)CATransform3DTranslate ? Translate ‘t’ by ‘(tx, ty, tz)’ and return the result: * t’ = translate(tx, ty, tz) * t. (3)CATransform3DMakeScale ? Returns a transform that scales by `(sx, sy, sz)’: * t’ = [sx 0 0 0; 0 sy 0 0; 0 0 sz 0; 0 0 0 1]. (4)CATransform3DScale ? Scale ‘t’ by ‘(sx, sy, sz)’ and return the result: * t’ = scale(sx, sy, sz) * t. (5)CATransform3DMakeRotation ? Returns a transform that rotates by ‘a(chǎn)ngle’ radians about the vector ‘(x, y, z)’. If the vector has length zero the identity transform is returned. (6)CATransform3DRotate ? Rotate ‘t’ by ‘a(chǎn)ngle’ radians about the vector ‘(x, y, z)’ and return the result. t’ = rotation(angle, x, y, z) * t. ? 3.旋轉(zhuǎn)的單位采用弧度(radians),而不是角度(degress)。以下兩個函數(shù),你可以在弧度和角度之間切換。 ? CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}; ? CGFloat RadiansToDegrees(CGFloat radians) {return radians * 180 / M_PI;}; ?? 4.核心動畫提供了用于轉(zhuǎn)換矩陣的變換函數(shù)CATransform3DInvert。一般是用反轉(zhuǎn)點內(nèi)轉(zhuǎn)化對象提供反向轉(zhuǎn)換。當(dāng)你需要恢復(fù)一個已經(jīng)被變換了的矩陣的時候,反轉(zhuǎn)將會非常有幫助。反轉(zhuǎn)矩陣乘以逆矩陣值,結(jié)果是原始值。
5.變換函數(shù)同時允許你把CATransform3D矩陣轉(zhuǎn)化為CGAffineTransform(仿射)矩陣,前提是CATransform3D矩陣采用如下表示方法。
6.CATransform3D 與 CGAffineTransform 轉(zhuǎn)換 (1)CATransform3DMakeAffineTransform ? Returns a CATransform3D with the same effect as the passed affine transform. (2)CATransform3DIsAffine ? Returns YES if the passedCATransform3D can be exactly represented an affine transform. (3)CATransform3DGetAffineTransform ? Returns the affine transform represented by the passedCATransform3D. ? 7.變換函數(shù)同時提供了可以比較一個變換矩陣是否是單位矩陣,或者兩個矩陣是否相等。
8.CATransform3D 相等測試 (1)CATransform3DIsIdentity ? Returns YES if the transform is the identity transform. (2)CATransform3DEqualToTransform ? Returns YES if the two transforms are exactly equal.. ? 五、圖層的集合變換-修改變換的數(shù)據(jù)結(jié)構(gòu)
1.你可以修改CATransform3D的數(shù)據(jù)結(jié)構(gòu)的元素為任何其他你想要的數(shù)據(jù)值。下面代碼包含了CATransform3D數(shù)據(jù)結(jié)構(gòu)的定義,結(jié)構(gòu)的成員都在其相應(yīng)的矩陣位置。 ? struct CATransform3D ? { ? ? CGFloat m11, m12, m13, m14; ? ? CGFloat m21, m22, m23, m24; ? ? CGFloat m31, m32, m33, m34; ? ? CGFloat m41, m42, m43, m44; ? }; ? typedef struct CATransform3D CATransform3D; ?? 2.下面代碼中的示例說明了如何配置一個CATransform3D一個角度變換 ? CATransform3D aTransform = CATransform3DIdentity; ? //the value of zDistance affects the sharpness of the transform. ? zDistance = 850; ? aTransform.m34 = 1.0 / -zDistance; ?? 六、圖層的集合變換-通過鍵值路徑修改變換
1.核心動畫擴(kuò)展了鍵-值編碼協(xié)議,允許通過關(guān)鍵路徑獲取和設(shè)置一個圖層的CATransform3D矩陣的值。下面描述了圖層的transform和sublayerTransform屬性的相應(yīng)關(guān)鍵路徑。 (1)rotation.x ? The rotation, in radians, in the x axis. (2)rotation.y ? The rotation, in radians, in the y axis. (3)rotation.z ? The rotation, in radians, in the z axis. (4)rotation ? The rotation, in radians, in the z axis. This is identical to setting the rotation.z field. (5)scale.x ? Scale factor for the x axis. (6)scale.y ? Scale factor for the y axis. (7)scale.z ? Scale factor for the z axis. (8)scale ? Average of all three scale factors. (9)translation.x ? Translate in the x axis. (10)translation.y ? Translate in the y axis. (11)translation.z ? Translate in the z axis. (12)translation ? Translate in the x and y axis. Value is an NSSize or CGSize.
2.你不可以通過Objective-C 2.0的屬性來設(shè)置結(jié)構(gòu)域的值,比如下面的代碼將會無法正常運行: ? myLayer.transform.rotation.x=0; ? 3.替換的辦法是,你必須通過setValue:forKeyPath:或者valueForKeyPath:方法,具體如下: ? [myLayer setValue:[NSNumber numberWithInt:0] forKeyPath:@"transform.rotation.x"];
轉(zhuǎn)自夢維:http://www.dreamingwish.com/dream-2012/coreanimation-programming-guide-c-the-geometric-transformation.html
總結(jié)
以上是生活随笔為你收集整理的CoreAnimation编程指南(三)几何变换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中使用lua操作redis
- 下一篇: 常用Mac小命令