2

有没有一种简单的方法来指定图像在物镜 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];
4

2 回答 2

2

您可以通过动画图层属性来做到这一点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)演员表。

于 2012-02-05T20:01:51.580 回答
0

我的车速表也有同样的问题。为变换属性设置动画时不能指定旋转方向。它总是会走最短的路。如果角度大于 180 度,我通过做 2 个动画解决了这个问题(第 1 个完成后开始第 2 个动画)。您必须注意正确的动画时间,以使其动画流畅。

于 2012-02-05T19:48:08.567 回答