CGAffineTransform旋转方向错误 [英] CGAffineTransform rotation goes the wrong way
问题描述
在我的实验中,我得出以下结论:
YourView.transform = CGAffineTransformMakeRotation(正值);
将顺时针旋转视图,
YourView.transform = CGAffineTransformMakeRotation(负值);
将逆时针旋转视图,
但该文件说:
以弧度表示旋转仿射变换的角度。在iOS中,正值指定逆时针旋转,负值指定顺时针旋转。
那些是否相互矛盾?
你的困惑是可以理解的。
事实上,正角度表示从正X轴朝向正Y轴。负角度表示从正X轴向负Y轴的旋转。
原生核心图形坐标系建立在标准解释了这一点。)你可以通过查看当前的变换矩阵来检查这一点(使用 CGContextGetCTM
)。您会发现它的 d
元素中有 -1
,这意味着Y轴被翻转。
A UIView
还使用翻转坐标系来布置其子视图,这会影响<$ c $的含义c> UIView 转换
属性。
In my experiment, i conclude these:
YourView.transform = CGAffineTransformMakeRotation( positive value );
will rotate the view clockwise, and
YourView.transform = CGAffineTransformMakeRotation( Negative value );
will rotate the view counterclockwise,
But the document says:
The angle, in radians, by which to rotate the affine transform. In iOS, a positive value specifies counterclockwise rotation and a negative value specifies clockwise rotation.
does those contradict with each other?
Your confusion is quite understandable.
In truth, a positive angle represents a rotation from the positive X axis toward the positive Y axis. A negative angle represents a rotation from the positive X axis toward the negative Y axis.
The "native" Core Graphics coordinate system is modeled after the standard Cartesian coordinate system, in which the Y axis increases upward on the page. In this system, a positive angle represents a counter-clockwise rotation:
So if you create your own CGContext
(for example, by using CGBitmapContextCreate
or CGPDFContextCreate
), rotations will work as you expect.
However, computer systems have historically used a coordinate system in which the Y axis increases downward on the page. In a flipped coordinate system like this, a positive angle represents a clockwise rotation:
Notice that in both coordinate systems, a positive angle rotates from the positive X axis toward the positive Y axis.
It turns out that UIKit flips the coordinate system of the graphics contexts that it creates for you. This includes the graphics context it sets up before sending you drawRect:
and the graphics context it sets up in UIGraphicsBeginImageContext
. (The Quartz 2D Programming Guide explains this.) You can check this by looking at the current transform matrix (using CGContextGetCTM
). You will find that it has a -1
in its d
element, meaning that the Y axis is flipped.
A UIView
also uses a flipped coordinate system for laying out its subviews, which affects the meaning of the UIView
transform
property.
这篇关于CGAffineTransform旋转方向错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!