CoreAnimation旋转变换动画方向
有没有一种简单的方法来指定图像在目标 C 中应该顺时针还是逆时针旋转?我有一个带有指针的速度计(一种反映汽车价格的设计) - 它工作正常,除非指针向下旋转并从屏幕转到另一侧更接近。我希望它始终逆时针旋转以转到较低的数字,顺时针旋转以转到较高的数字。这是我的代码段:
// figure out the minimum and maximum prices shown on the
// speedometer
float min = [_a.text floatValue] * 1000;
float max = [_g.text floatValue] * 1000;
// set calibration between number labels to be small or large
int cal = SMALL_CALIBRATION;
if ([_stickerPriceSetting floatValue] >= TRIGGERPRICE)
cal = LARGE_CALIBRATION;
// set an animation duration of 1.5 seconds for the needle rotation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];
// low prices (off speedometer)
if (price < (min - cal))
_needle.transform = CGAffineTransformMakeRotation(- M_PI/12);
// price too high (off speedometer)
else if (price > (max + cal))
_needle.transform = CGAffineTransformMakeRotation(M_PI * 13/12);
// need to have needle point to the price
else {
float delta = (max - min);
float price_point = price - min;
_needle.transform = CGAffineTransformMakeRotation(price_point/delta * M_PI);
}
[UIView commitAnimations];
Is there a simple way to specify whether an image should rotate clockwise or anti-clockwise in objective C? I have a speedometer with a needle (a design that reflects prices of automobiles) - it is working fine, except when it is closer for the needle to rotate down and out of the screen over to the other side. I want it always to rotate counterclockwise to go to lower numbers and clockwise to go to higher numbers. Here is my code segment:
// figure out the minimum and maximum prices shown on the
// speedometer
float min = [_a.text floatValue] * 1000;
float max = [_g.text floatValue] * 1000;
// set calibration between number labels to be small or large
int cal = SMALL_CALIBRATION;
if ([_stickerPriceSetting floatValue] >= TRIGGERPRICE)
cal = LARGE_CALIBRATION;
// set an animation duration of 1.5 seconds for the needle rotation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];
// low prices (off speedometer)
if (price < (min - cal))
_needle.transform = CGAffineTransformMakeRotation(- M_PI/12);
// price too high (off speedometer)
else if (price > (max + cal))
_needle.transform = CGAffineTransformMakeRotation(M_PI * 13/12);
// need to have needle point to the price
else {
float delta = (max - min);
float price_point = price - min;
_needle.transform = CGAffineTransformMakeRotation(price_point/delta * M_PI);
}
[UIView commitAnimations];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过动画图层属性
transform.rotation
来完成此操作:如果导入
QuartzCore/QuartzCore.h
,您将不需要(id)
演员。You can do it by animating the layer property
transform.rotation
:If you import
QuartzCore/QuartzCore.h
you won't need the(id)
cast.我的车速表也有同样的问题。在对变换属性进行动画处理时,您无法指定旋转方向。它总是会走最短的路。我通过做 2 个动画来解决这个问题,以防角度大于 180 度(第二个动画在第一个动画完成后开始)。您必须注意正确的动画时间,以便动画流畅。
I had the same problem with a speedometer. You cannot specify the rotation direction when animating the transform-property. It will always take the shortest way. I solved the problem by doing 2 animations in case the angle is greater than 180 degrees (2nd animation starts after 1st finishes). You have to pay attention for the correct animation times so that it animates smooth.