2

Objective 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

スピードメーターにも同じ問題がありました。transform-property をアニメーション化するときは、回転方向を指定できません。必ず最短距離になります。角度が 180 度を超える場合に備えて、2 つのアニメーションを実行することで問題を解決しました (1 つ目のアニメーションが終了した後に 2 つ目のアニメーションが開始されます)。アニメーションが滑らかになるように、正しいアニメーション時間に注意を払う必要があります。

于 2012-02-05T19:48:08.567 に答える