2

目的のCで画像を時計回りにするか反時計回りに回転するかを指定する簡単な方法はありますか?私は針でスピードメーターを持っています(自動車の価格を反映したデザイン) - それはニードルが画面の下側に回転し、反対側に回転している場合を除いて、うまくいきます。反時計回りに回すと数字が小さくなり、時計回りに回すと数字が上がります。私のコードセグメントは次のとおりです:CoreAnimation回転変換アニメーションの方向

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

答えて

0

私はスピードメーターで同じ問題がありました。 transformプロパティをアニメートするときは、回転方向を指定することはできません。それは常に最短の方法で行われます。角度が180度より大きい場合(2回目のアニメーションは1回目の終了後に開始)、2つのアニメーションを実行して問題を解決しました。あなたはアニメーションが滑らかになるように正しいアニメーション時間に注意を払わなければなりません。

+0

これは、元の点から新しい点までの角度が180度より大きくなるかどうかを知る方法がないことを除いて、私には有益です - 最初にどの角度を見つけるかが分からない限り針(UIView)は現在ですか?私は、古い価格を保存し、それを針の動きに取り入れる一連のコードを書く必要があると推測しています。 –

2

あなたは、レイヤプロパティtransform.rotationアニメーションでそれを行うことができます:あなたは(id)キャストを必要としませんQuartzCore/QuartzCore.hインポートする場合

// 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"]; 
}]; 

を。

関連する問題