Affaine Transformation只要是用在2D圖像變換上的的。比如縮放,平移,旋轉,扭曲。常用的CATransform3D道理相同
概述
Affine Transformation由一個3 x 3矩陣來表示
作為一個增廣矩陣,第三列永遠是[0 0 1]。 所以在API中提供的都是對前兩列參數的改變。
下圖表示的即是變換過程,圖像上的每個點的坐標(x, y, 1),與上圖矩陣相乘,產生新的坐標(x', y', 1),也就是我們變化后的圖像上點的坐標。
對于圖像上的每個點,通過這樣的計算轉換到了新的坐標。
通常不需要直接創建affine transformation, 只需要調用對應的函數 translateBy(x:y:)
, scaleBy(x:y:)
, rotate(by:)
即可
變換
如果自定義創建的話,一般都是先創建一個單位矩陣
let indentity = CATransform3DIdentity // [1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1].
平移
let transformation = CATransform3DTranslate(transform, tx, ty, tz)
這個變換實際上發生的是 (代碼上是3D,我們的例子跟圖上的矩陣保持一致,用2D)
x' = ax + cy + tx
y' = bx + dy + ty
所以顯而易見,tx, ty, tz
就是平移移動的距離了
CATransform3DTranslate(transform, tx, ty, tz)