2012-01-09 5 views
1

私は、垂直バーである右側にUIViewオブジェクト(ゲージと呼ばれる)を持っています。私はそのバーの左に、垂直バーの異なるレベル(navLabel)に異なるテキストを表示したい別のラベルがあります。私はこのラベルを垂直バーにある程度(10 pxのような)固定されているように見せたい。私のラベルは左揃えです。私はちょうど適切な高さ(すなわち#define FIRST_HEIGHT)に変更し、私の異なる方法で別のUIViewオブジェクトへのUILabelアニメーション

 CGSize labelSize = [navLabel.text sizeWithFont:[UIFont fontWithName:@"Arial" size:17.0]]; 
     CGRect newFrame = CGRectMake(gauge.frame.origin.x - 10 - labelSize.width, FIRST_HEIGHT, navLabel.frame.size.width, navLabel.frame.size.height); 
     [UIView animateWithDuration:0.5 animations:^{ 
      self.navLabel.frame = newFrame; 
     }]; 

:私はラベルを更新する必要がさまざまな方法でこれを行います。

私のラベルは、私が欲しい場所で終わるが、アニメーションは面白く見える。たとえば、最初のラベルが@"label 1"で、2番目のラベルが@"my much much much longer label"だった場合、ラベルは正しい高さになりますが、ラベルのサイズが異なるため、xアニメーションも表示され、面白く見えます。私はy方向のアニメーションだけを望んでいます。どうすればそれを達成できますか?

また、私は右寄せを試みましたが、私のラベルはゲージオブジェクトとの正しい距離に達しません。それは何回もそれを越えて終わり、私はなぜそれが起こるか分からない。

答えて

0

これはあなたが望む行動であることは明らかではないのですが、なぜちょうど正しい(新しい)その原点としてx座標前にアニメーションを持つようにnavLabel.frameを設定していないし、正しい起源を持つ新しいフレームにアニメーション化新しい高さはオフセットされていますか?

または、これらの2つのフレームを使用して2つのアニメーションを設定します.1つはy方向を最初にアニメーション化し、もう1つはx方向をフレーム原点の新しいx座標にアニメートします。またはその逆。

// This version simply sets the correct origin (and the correct new width), then animates the y-direction 
CGSize labelSize = [navLabel.text sizeWithFont:[UIFont fontWithName:@"Arial" size:17.0]]; 
self.navLabel.frame = CGRectMake(gauge.frame.origin.x-10-labelSize.width, self.navLabel.frame.origin.y,labelSize.width,navLabel.frame.size.height); 
// now navLabel's frame has the correct new width (and origin x-coord) 
CGRect newFrame = CGRectMake(self.navLabel.origin.x, FIRST_HEIGHT, navLabel.frame.size.width, navLabel.frame.size.height); 
// now this animation will cause movement only in the y-direction 
[UIView animateWithDuration:0.5 animations:^{ 
     self.navLabel.frame = newFrame; 
    }]; 

// This version animates the change in the frame in the x-direction first, then does what it did before 
CGSize labelSize = [navLabel.text sizeWithFont:[UIFont fontWithName:@"Arial" size:17.0]]; 
CGRect newFrame = CGRectMake(gauge.frame.origin.x-10-labelSize.width, self.navLabel.frame.origin.y,labelSize.width,navLabel.frame.size.height); 
// this animation will cause movement in the x-direction 
[UIView animateWithDuration:0.5 animations:^{ 
     self.navLabel.frame = newFrame; 
    }]; 
// now navLabel's frame has the correct new width (and origin x-coord), so set the new y-coord 
CGRect newFrame = CGRectMake(self.navLabel.origin.x, FIRST_HEIGHT, navLabel.frame.size.width, navLabel.frame.size.height); 
// now this animation will cause movement only in the y-direction 
[UIView animateWithDuration:0.5 animations:^{ 
     self.navLabel.frame = newFrame; 
    }]; 
+0

y座標またはx座標だけをアニメーション化するにはどうすればよいですか?その値だけを変更してフレームを作成し、UIViewアニメーションブロックで新しいフレームを設定しますか?どうも。 – Crystal

+0

はい、正確です。あなたのフレームの違いがこれらの座標の* 1 *だけであれば、そのすべてがアニメーション化されます。しかし、アニメーションの前後にフレームを設定するだけで、更新されたx座標を起点とすると面白く見えるかもしれません。そのため、おそらく動作に惑わされて見栄えの良いものを得る必要があります。または、私が示唆したように、あなたは一つの "方向"をアニメーション化し、次にもう一つをアニメートすることができます。コード例については、私の更新答えを見てください。 – ddodev

関連する問題