CoreAnimation旋转变换动画方向

时间:2012-02-05 19:38:45

标签: objective-c core-animation cgaffinetransform image-rotation

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

2 个答案:

答案 0 :(得分: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)演员。

答案 1 :(得分:0)

我在车速表上遇到了同样的问题。动画转换属性时,无法指定旋转方向。它总是采用最短的方式。我在角度大于180度的情况下通过做2个动画来解决问题(第1个动画在第1个完成后开始)。您必须注意正确的动画时间,以使其动画流畅。