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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 20:52:10  来源:igfitidea点击:

How to "rotate" a layer/view (e.g. just like you would in enigmo)

iphoneobjective-ccocoa-touch

提问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

您将使用视图的转换属性。iPhone OS 编程指南中有一些用于旋转视图的示例代码,在横向模式启动

回答by Louis Gerbarg

You should look at Apple's MoveMeexample for how to move around a layer based on touch. It also applies some scaling transforms as you do it, so that should serve as a reasonable example of to apply rotation transforms.

您应该查看 Apple 的MoveMe示例,了解如何基于触摸在图层上移动。它还会在您执行此操作时应用一些缩放变换,因此这应该作为应用旋转变换的合理示例。