CTM transforms vs Affine Transforms in iOS (for translate, rotate, scale)

末鹿安然 提交于 2019-12-21 23:08:16

问题


I read through the Transforms documentation in the Quartz 2D Programming Guide. In it there appears to be two ways to make transformations. One way is through modifying the Current Transformation Matrix (CTM). It has methods like the following:

  • CGContextTranslateCTM
  • CGContextRotateCTM
  • CGContextScaleCTM

The other way is to use Affine transforms. It has methods like the following:

  • CGAffineTransformTranslate
  • CGAffineTransformRotate
  • CGAffineTransformScale

The docs state

The affine transform functions available in Quartz operate on matrices, not on the CTM.

But I don't understand how that affects me practically. It seems like I can get the same result using either method. When should I use the CTM transforms and when should I use the Affine transforms?


回答1:


CTM is a current transformation matrix and the CTM methods will make operations on the current matrix.

The other version of functions will make the transformation on a given matrix which means you need to specify which matrix you are trying to transform. After you did so you may apply the transform to the CTM any way you want or use it for any other purpose.

For instance these 2 operations would be the same:

CGContextTranslateCTM(context, 10, 10);

Affine:

CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, 10, 10);
CGContextConcatCTM(context, transform);

As you can see the first one is more or less just a convenience so you do not need to write so much code.



来源:https://stackoverflow.com/questions/30756907/ctm-transforms-vs-affine-transforms-in-ios-for-translate-rotate-scale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!