CoreAnimation旋转变换动画方向

发布于 2025-01-03 11:10:35 字数 1148 浏览 3 评论 0原文

有没有一种简单的方法来指定图像在目标 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 技术交流群。

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

发布评论

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

评论(2

从﹋此江山别 2025-01-10 11:10:35

您可以通过动画图层属性 transform.rotation 来完成此操作:

// 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;

CGFloat angle;
if (price < min - cal)
    angle = -M_PI / 12;
else if (price > max + cal)
    angle = M_PI * 13 / 12;
else
    angle = M_PI * (price - min) / (max - min);

[UIView animateWithDuration:1.5 animations:^{
    [(id)_needle.layer setValue:[NSNumber numberWithFloat:angle]
        forKeyPath:@"transform.rotation"];
}];

如果导入 QuartzCore/QuartzCore.h,您将不需要 (id) 演员。

You can do it by animating the layer property transform.rotation:

// 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;

CGFloat angle;
if (price < min - cal)
    angle = -M_PI / 12;
else if (price > max + cal)
    angle = M_PI * 13 / 12;
else
    angle = M_PI * (price - min) / (max - min);

[UIView animateWithDuration:1.5 animations:^{
    [(id)_needle.layer setValue:[NSNumber numberWithFloat:angle]
        forKeyPath:@"transform.rotation"];
}];

If you import QuartzCore/QuartzCore.h you won't need the (id) cast.

一紙繁鸢 2025-01-10 11:10:35

我的车速表也有同样的问题。在对变换属性进行动画处理时,您无法指定旋转方向。它总是会走最短的路。我通过做 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.

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