xcode Xcode顺时针旋转uibutton

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5050143/
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-14 20:16:09  来源:igfitidea点击:

Xcode rotate uibutton clockwise

xcodeios4uibuttonrotation

提问by CristiC

I want to rotate an UIButton at 180 degrees clockwise. But it always rotate counterclockwise.

我想将 UIButton 顺时针旋转 180 度。但它总是逆时针旋转。

This is how I tried:

这是我尝试的方式:

CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.3];

myButton.transform = CGAffineTransformRotate( myButton.transform, M_PI);

[UIView commitAnimations];

also this:

还有这个:

myButton.transform = CGAffineTransformRotate( myButton.transform, - M_PI);

What am I doing wrong?

我究竟做错了什么?

回答by Lecrte

I've had a similar experience, and my best guess is the following:

我有过类似的经历,我最好的猜测如下:

The rotation transform translates to a net result, meaning an absolute rotation. Since rotating -PI and +PI results in the same net effect (both 180 degrees), the animation ends up always choosing the default direction; which seems to be counterclockwise on iOS.

旋转变换转化为净结果,即绝对旋转。由于旋转 -PI 和 +PI 会产生相同的净效果(均为 180 度),因此动画最终总是选择默认方向;这在 iOS 上似乎是逆时针的。

By setting it to a value slightly more negative than -M_PI, as @kishorebjv mentioned, the shortest rotation path is through the positive direction (switching the animation to clockwise). You can see this effect by using M_PI+0.01 or M_PI-0.01. Both are positive numbers, but they result in different directions.

正如@kishorebjv 提到的,通过将其设置为比 -M_PI 稍负的值,最短的旋转路径是通过正方向(将动画切换为顺时针方向)。您可以使用 M_PI+0.01 或 M_PI-0.01 来查看此效果。两者都是正数,但它们导致不同的方向。

More verbose explanation: Value: M_PI+0.01 Direction: Counterclockwise Reasoning: This is this translates to a rotation of ~180.6, which the shortest rotation is thus a negative 179.4 degrees.

更详细的解释:值:M_PI+0.01 方向:逆时针 推理:这转化为 ~180.6 的旋转,因此最短的旋转是负 179.4 度。

Value: M_PI-0.01
Direction: Clockwise
Reasoning: This is this translates to a rotation of ~179.4, 
which the shortest rotation is thus a positive 179.4 degrees.

And going back to the value given by kishorebjv
Value: -3.141593
Direction: Clockwise
Reasoning: The value is slightly past -180 degrees, recalling PI is 3.1415926
....so the shortest rotation is a positive 179 degrees

回答by kishorebjv

I'm surprised..I don't know why its happening like that.

我很惊讶..我不知道为什么会这样。

Instead of -M_PI give -3.141593.

而不是 -M_PI 给 -3.141593。

It 'll rotate clockwise..

它会顺时针旋转..

as of now its a quick fix.but probably not a exact answer for your question

到目前为止,它是一个快速修复。但可能不是您问题的确切答案