Xcode将UIButton顺时针旋转

8

我想把一个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];

also this:

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

我做错了什么?


你是在开发iOS还是OSX应用? 如果是iOS应用,CGAffineTransformRotate函数应该使用正角度进行逆时针旋转,使用负角度进行顺时针旋转。 - mmccomb
这是针对iOS的,但正如我所说,我尝试了正负符号,但没有任何效果。它仍然是逆时针旋转的。 - CristiC
2个回答

16

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

旋转变换会转化为一种净效果,也就是绝对旋转。由于将-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

6

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

不要使用-M_PI,而是使用-3.141593。

它将顺时针旋转...

目前这只是一个快速修复,但可能并不是你问题的确切答案。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接