9

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?

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
romox
  • 176
  • 1
  • 7

1 Answers1

25

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:

normal coordinate system

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:

flipped coordinate system

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.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • 1
    but in a flipped coordinate system like that, a positive angle represents a clockwise rotation, but the documents say "In iOS, a positive value specifies counterclockwise rotation." So i think it's still contradictive. Is the document wrong? – romox Jul 31 '12 at 09:14
  • It's wrong sometimes. It depends on the context in which you use the transform. – rob mayoff Jul 31 '12 at 19:11
  • @romox It is likely that you were reading something like [this](https://developer.apple.com/reference/coregraphics/1455666-cgaffinetransformmakerotation). That is a CoreGraphics documentation, but you are in UIKit coordinate system, not in a Core Graphics coordinate system so everything seems correct. Read more in this [SO post](http://stackoverflow.com/a/41126766/3402095). – Whirlwind Dec 13 '16 at 17:48