具有负M_PI的CGAffineTransformMakeRotation [英] CGAffineTransformMakeRotation with negative M_PI
问题描述
我正在使用 CGAffineTransformMakeRotation
在动画块内旋转 UIView
,我想逆时针旋转它对于180º
。
但是当我把
<$ p时$ p>
myView.animateWithDuration(0.5)
myView.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI))
它仍然顺时针旋转。令我困惑的是,如果我手动写入 M_PI
的值,它会按预期工作
//这将逆时针旋转
myView.transform = CGAffineTransformMakeRotation(CGFloat(-3.14159265358979))
为什么会出现这种情况?
编辑:添加我正在使用动画块
正如我在第一篇评论中所说,并且Segii已经写过:动画将采用它现在的价值和试图去的地方之间的最短距离。最好的解决方案是使用 CABasicAnimation
来强制逆时针旋转:
让anim = CABasicAnimation(keyPath:transform.rotation)
anim.fromValue = M_PI
anim.toValue = 0
anim.additive = true
anim.duration = 2.0
myView.layer.addAnimation(anim,forKey:rotate)
myView.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI))
这将一次逆时针旋转180度。
你指定的角度最后一行+ fromValue
是起始角度 - > 0.然后旋转距离(toValue - fromValue)= 0 - M_PI = - M_PI
- >逆时针旋转180度。
I'm using CGAffineTransformMakeRotation
to rotate a UIView
inside an animation block, and I want to rotate it counterclockwise for 180º
.
However when I put
myView.animateWithDuration(0.5)
myView.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI))
it still rotates clockwise. What's confusing me is that if I write "manually" the value of M_PI
, it works as expected
// this will rotate counterclockwise
myView.transform = CGAffineTransformMakeRotation(CGFloat(-3.14159265358979))
Why this behavior?
Edit: added I'm using an animation block
As I stated in my first comment and as Segii already wrote: the animation will take the shortest distance between where it's value is at now and where it is trying to go. The best solution imho is to use CABasicAnimation
for forcing a counterclockwise rotation:
let anim = CABasicAnimation(keyPath: "transform.rotation")
anim.fromValue = M_PI
anim.toValue = 0
anim.additive = true
anim.duration = 2.0
myView.layer.addAnimation(anim, forKey: "rotate")
myView.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI))
This will rotate it by 180 counter clockwise in one go.
The angle you specify in the last line + the fromValue
is the starting angle -> 0. And then the rotation will go the distance (toValue - fromValue) = 0 - M_PI = - M_PI
-> counter clockwise 180 degree rotation.
这篇关于具有负M_PI的CGAffineTransformMakeRotation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!