1

我正在尝试制作一个在按下时会增长的 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

2 回答 2

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);
于 2009-07-13T23:08:50.423 回答
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;
于 2009-07-13T23:08:47.507 回答