【问题标题】:UIButton moves when pressedUIButton 按下时移动
【发布时间】:2009-07-13 23:04:15
【问题描述】:

我正在尝试制作一个按下时会增长的 UIButton。目前我在按钮的按下事件中有以下代码(Tim's answer 提供):

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

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

【问题讨论】:

    标签: cocoa-touch uibutton


    【解决方案1】:

    你可以使用CGRectInset:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2011-04-09
        • 2012-06-26
        • 1970-01-01
        • 2014-04-12
        • 2012-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多