# 【深入iOS開發】iOS仿射變換和3D變換

## 2D仿射變換

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

Paste_Image.png

#### 單位矩陣

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)``````

Paste_Image.png