NO IMAGE

2D仿射變換

UIView的transform屬性是一個CGAffineTransform型別,用於在二維空間做旋轉,縮放和平移。CGAffineTransform是一個可以和二維空間向量(例如CGPoint)做乘法的3X2的矩陣:

1240

Paste_Image.png

當對UIView應用變換矩陣,UIView圖層矩形內的每一個點都被相應地做變換,從而形成一個新的四邊形的形狀。CGAffineTransform中的“仿射”的意思是無論變換矩陣用什麼值,圖層中平行的兩條線在變換之後任然保持平行.

單位矩陣

CGAffineTransformIdentity

CGAffineTransform transform = CGAffineTransformIdentity;
結果:
(CGAffineTransform) transform = (a = 1, b = 0, c = 0, d = 1, tx = 0, ty = 0)

建立仿射變換矩陣

CGAffineTransformMakeRotation(CGFloat angle)  // 旋轉
CGAffineTransformMakeScale(CGFloat sx, CGFloat sy) // 縮放
CGAffineTransformMakeTranslation(CGFloat tx, CGFloat ty)  // 平移
CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI_4);
結果 :
(CGAffineTransform) transform = (a = 0.70710678118654757, b = 0.70710678118654746, c = -0.70710678118654746, d = 0.70710678118654757, tx = 0, ty = 0)
CGAffineTransform transform = CGAffineTransformMakeScale(0.8, 0.8);
結果:
(CGAffineTransform) transform = (a = 0.80000000000000004, b = 0, c = 0, d = 0.80000000000000004, tx = 0, ty = 0)
CGAffineTransform transform = CGAffineTransformMakeTranslation(10, 10);
結果:
(CGAffineTransform) transform = (a = 1, b = 0, c = 0, d = 1, tx = 10, ty = 10)
1240

Paste_Image.png

閱讀全文直接點選:點選開啟連結