Cocoa touch 按钮按下时会移动

Cocoa touch 按钮按下时会移动,cocoa-touch,uibutton,Cocoa Touch,Uibutton,我试着做一个按钮,当按下时它会变大。目前,我的按钮关闭事件中有以下代码(由提供): 但是,当运行时,每次按下按钮时,我的按钮都会向上和向左移动。有什么想法吗?我打赌你需要一些括号。记住除法发生在加法/减法之前 此外,CGRectMake的前两个参数指示按钮在屏幕上的位置,后两个参数指示大小。因此,如果您只想更改按钮大小,只需设置最后两个参数 #define button_grow_amount 1.2 CGRect currentFrame = button.frame; CGRect newF

我试着做一个按钮,当按下时它会变大。目前,我的按钮关闭事件中有以下代码(由提供):


但是,当运行时,每次按下按钮时,我的按钮都会向上和向左移动。有什么想法吗?

我打赌你需要一些括号。记住除法发生在加法/减法之前

此外,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;

我打赌你需要一些括号。记住除法发生在加法/减法之前

此外,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;
您可以使用:

您可以使用:

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