objective-c 如何“旋转”图层/视图(例如,就像在 enigmo 中一样)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/242424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to "rotate" a layer/view (e.g. just like you would in enigmo)
提问by rksprst
I know how to move a layer based on touch. But I would also like to be able to rotate the image.
我知道如何根据触摸移动图层。但我也希望能够旋转图像。
Is there any sample code that shows how to do this? Or can anyone give me some advice?
是否有任何示例代码显示如何执行此操作?或者谁能给我一些建议?
Thanks!
谢谢!
回答by Ben Gottlieb
The simplest way to do this is using the layer's transform property:
最简单的方法是使用图层的变换属性:
float angle = M_PI; //rotate 180°, or 1 π radians
layer.transform = CATransform3DMakeRotation(angle, 0, 0.0, 1.0);
The first argument to the CATransform3DMakeRotation function is the amount to rotate, in radians. The next three describe the vector around which to rotate. This is describing a vector in the z-axis, so effectively perpendicular to the screen. This will rotate the layer so it's upside down.
CATransform3DMakeRotation 函数的第一个参数是旋转量,以弧度为单位。接下来的三个描述了旋转的向量。这是在 z 轴上描述一个向量,因此有效地垂直于屏幕。这将旋转图层,使其上下颠倒。
回答by rksprst
I ended up doing it like this:
我最终这样做了:
CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
[[self viewWithTag:999] setTransform:transform];
Note that the angle is in radians.
请注意,角度以弧度为单位。
回答by Jesús A. álvarez
You would use the view's transform property. There's some example code for rotating the view in the iPhone OS Programming Guide, under Launching in Landscape Mode

