About CGAffineTransformMakeRotation rotation direction?

后端 未结 5 467
花落未央
花落未央 2020-12-13 14:52

I\'m trying to use CGAffineTransformMakeRotation, and I refer Apple document which says

The angle, in radians, by which this matrix rotates the coordi

5条回答
  •  囚心锁ツ
    2020-12-13 15:25

    This is actually correct behavior, and the documentation describes it correctly.

    You're dealing with two different coordinate systems here. Core Graphics' coordinate system has (0, 0) on the bottom left, but UIKit's coordinate system has (0, 0) on the top left. Both coordinate systems have an X-axis with 0 on the left. In other words, UIKit's coordinate system is the Core Graphics coordinate system flipped vertically. The following image demonstrates the difference:


    Now, let's look at your quote:

    The angle, in radians, by which this matrix rotates the coordinate system axes. In iOS, a positive value specifies counterclockwise rotation and a negative value specifies clockwise rotation.

    At first glance that seems wrong, but you've missed something. This is a quote from the CGAffineTransformMakeRotation reference. This is part of the Core Graphics reference, not the UIKit reference. It's referring to the rotation as counterclockwise in Core Graphics' coordinate system.

    You can picture this rotation in your head by flipping what you're seeing on screen vertically and imagining what the rotation would look like.

    In fact, the CGAffineTransformMakeRotation reference has a discussion item which goes on to document this:

    The actual direction of rotation is dependent on the coordinate system orientation of the target platform, which is different in iOS and macOS.


    The discussion item on CGContext's rotate(by:) method reference describes this behavior a little better:

    The direction that the context is rotated may appear to be altered by the state of the current transformation matrix prior to executing this function. For example, on iOS, a UIView applies a transformation to the graphics context that inverts the Y-axis (by multiplying it by -1). Rotating the user coordinate system on coordinate system that was previously flipped results in a rotation in the opposite direction (that is, positive values appear to rotate the coordinate system in the clockwise direction).

提交回复
热议问题