Cin*_*ndy 2 cgaffinetransform ios
我需要从原点(250,250)到原点(352,315)的变换视图,宽度/高度从(100.0,100.0)变为(68,68).我知道我可以将几个CGAffineTransform函数组合在一起,例如缩放,旋转,平移.但我不知道如何计算这些变换的顺序,以及它们的确切参数.我已经尝试了几次,但无法将视图移动到正确的位置.
有人可以帮忙吗?
Mic*_*edo 10
在这些矩阵变换中,对幕后发生的事情有一点了解总是很好.
Apple文档有关于转换的很好的文档,所以让我们使用它.
一个转换矩阵是这样的:
| 1 0 0 |
| 0 1 0 |
| tx ty 1 |
Run Code Online (Sandbox Code Playgroud)
(tx, ty)你的翻译矢量在哪里?
一个缩放矩阵是这样的:
| sx 0 0 |
| 0 sy 0 |
| 0 0 1 |
Run Code Online (Sandbox Code Playgroud)
其中sx和sy是X轴和Y轴的比例因子.
您希望使用这些矩阵连接CGAffineTransformConcat,但根据其文档:
请注意,矩阵运算不是可交换的 - 连接矩阵的顺序很重要.也就是说,将矩阵t1乘以矩阵t2的结果不一定等于矩阵t2乘以矩阵t1的结果.
您必须在缩放视图之前翻译视图,否则您的翻译向量将根据sx和sy系数进行缩放.
让我们轻松地展示它:
let scaleMatrix = CGAffineTransformMakeScale(0.68, 0.68)
let translateMatrix = CGAffineTransformMakeTranslation(102, 65)
let translateThenScaleMatrix = CGAffineTransformConcat(scaleMatrix, translateMatrix)
NSLog("translateThenScaleMatrix : \(translateThenScaleMatrix)")
// outputs : CGAffineTransform(a: 0.68, b: 0.0, c: 0.0, d: 0.68, tx: 102.0, ty: 65.0)
// the translation is the same
let scaleThenTranslateMatrix = CGAffineTransformConcat(translateMatrix, scaleMatrix)
NSLog("scaleThenTranslateMatrix : \(scaleThenTranslateMatrix)")
// outputs : CGAffineTransform(a: 0.68, b: 0.0, c: 0.0, d: 0.68, tx: 69.36, ty: 44.2)
// the translation has been scaled too
Run Code Online (Sandbox Code Playgroud)
让我们用数学证明它.请注意,当您执行操作A然后执行操作B时,通过执行matB*matA计算相关矩阵,第一个操作在右侧.由于乘法对于矩阵不是可交换的,因此它很重要.
// Translate then scaling :
| sx 0 0 | | 1 0 0 | | sx 0 0 |
| 0 sy 0 | . | 0 1 0 | = | 0 sy 0 |
| 0 0 1 | | tx ty 1 | | tx ty 1 |
// The resulting matrix has the same value for translation
// Scaling then translation :
| 1 0 0 | | sx 0 0 | | sx 0 0 |
| 0 1 0 | . | 0 sy 0 | = | 0 sy 0 |
| tx ty 1 | | 0 0 1 | | sx.tx sy.ty 1 |
// The translation values are affected by scaling coefficient
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2157 次 |
| 最近记录: |