2009-07-13 83 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 2009-07-13 23:40:20

1

你需要一些圓括號在那裏我敢打賭。請記住,除法之前發生分割。

另外,CGRectMake的前兩個參數決定了按鈕在屏幕上的位置,而後兩個參數指示了尺寸。所以如果你只是想改變按鈕尺寸,只需設置最後兩個參數。

#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;