Xcode顺时针旋转uibutton

发布于 2024-10-18 02:13:13 字数 513 浏览 1 评论 0原文

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

这就是我尝试的方法:

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

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

[UIView commitAnimations];

也是这样:

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

我做错了什么?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

堇色安年 2024-10-25 02:13:13

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

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

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

更详细的解释:
值: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

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.

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.

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.

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
过期情话 2024-10-25 02:13:13

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

而不是 -M_PI 给出 ​​-3.141593。

它会顺时针旋转..

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

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

Instead of -M_PI give -3.141593.

It 'll rotate clockwise..

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文