2009-07-13 13 views
1

押したときに大きくなるUIButtonを作成しようとしています。現在、私は(Tim's answerの礼儀)私のボタンのダウンイベントに次のコードを持っている:実行しかし押したときにUIButtonが移動する

#define button_grow_amount 1.2 
CGRect currentFrame = button.frame; 
CGRect newFrame = CGRectMake(currentFrame.origin.x - currentFrame.size.width/button_grow_amount, 
           currentFrame.origin.y - currentFrame.size.height/button_grow_amount, 
           currentFrame.size.width * button_grow_amount, 
           currentFrame.size.height * button_grow_amount); 
button.frame = newFrame; 

この私のボタンを上に移動し、それが押されていますたびに左になります。何か案は?

答えて

4

あなたはCGRectInsetを使用することができます。

CGFloat dx = currentFrame.size.width * (button_grow_amount - 1); 
CGFloat dy = currentFrame.size.height * (button_grow_amount - 1); 
newFrame = CGRectInset(currentFrame, -dx, -dy); 
+0

これは働いていました!ありがとう! – RCIX

+0

私の側からも有益な感謝....投票.... – Saawan

1

私はそこにいくつかのカッコが必要です。加算と減算の前に除算が行われることに注意してください。

また、CGRectMakeの最初の2つのパラメータは、画面上でボタンがどこにあるかを指示し、2番目の2つはサイズを示します。ボタンのサイズを変更したい場合は、最後の2つのパラメータだけを設定します。

#define button_grow_amount 1.2 
CGRect currentFrame = button.frame; 
CGRect newFrame = CGRectMake((currentFrame.origin.x - currentFrame.size.width)/button_grow_amount, 
          (currentFrame.origin.y - currentFrame.size.height)/button_grow_amount, 
          currentFrame.size.width * button_grow_amount, 
          currentFrame.size.height * button_grow_amount); 

button.frame = newFrame; 
関連する問題