CGAffineTransform 旋转走错路了 [英] CGAffineTransform rotation goes the wrong way

查看:18
本文介绍了CGAffineTransform 旋转走错路了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的实验中,我得出以下结论:

YourView.transform = CGAffineTransformMakeRotation(正值);

将顺时针旋转视图,并且

YourView.transform = CGAffineTransformMakeRotation(负值);

将逆时针旋转视图,

但是文件说:

旋转仿射变换的角度,以弧度为单位.在 iOS 中,正值表示逆时针旋转,负值表示顺时针旋转.

这些是否相互矛盾?

解决方案

你的困惑是可以理解的.

实际上,正角度表示从正 X 轴向正 Y 轴的旋转.负角表示从正 X 轴向负 Y 轴旋转.

原生"Core Graphics 坐标系仿照标准 解释了这一点.)您可以通过查看当前变换矩阵来检查这一点(使用 CGContextGetCTM).你会发现它的d元素中有一个-1,表示Y轴翻转了.

UIView 也使用翻转坐标系来布置其子视图,这会影响 UIView transform 属性的含义.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆