【问题标题】:How to increase height of UIButton image without moving button如何在不移动按钮的情况下增加 UIButton 图像的高度
【发布时间】:2015-05-22 19:44:36
【问题描述】:

我已将UIButton 添加到情节提要,然后将我的图像添加到按钮。我要做的是,当您点击按钮时,高度会略微增加,然后按钮的高度会减少大约四分之一。

@IBAction func StopsClick(sender: UIView) {
    //Get the y coordinates of Image
    let origin = sender.bounds.origin.y

    //This decreases the height of Image but the image moved. I want the image to remain stationary and only the top of the image to increase in height.
    UIView.animateWithDuration(0.5) {
        sender.bounds = CGRectMake(0, origin, sender.bounds.width, sender.bounds.height * 1.2)
    }

    UIView.animateWithDuration(1.0) {
        sender.bounds = CGRectMake(0, orgin, sender.bounds.width, sender.bounds.height * 0.4)
    }
}

【问题讨论】:

    标签: swift uiview uibutton


    【解决方案1】:

    你应该能够得到你使用变换后的效果。

    我会做以下事情:

    从身份转换开始。 将变换的原点向下移动到图像的底部。 根据需要增加变换的 scale.y。 将变换的原点移回图像的中心

    将该变换应用于动画中的按钮。然后将其动画化回身份转换。

    如果您不移动变换并仅缩放它,它将从中心向所有方向增长。 (或者如果你只增加 scale.y 则只上下)

    编辑:

    代码可能如下所示:

    let halfHeight = button.bounds.height / 2
    
    //Make the center of the grow animation be the bottom center of the button
    var transform = CGAffineTransformMakeTranslation(0, -halfHeight)
    
    //Animate the button to 120% of it's normal height.
    tranform = CGAffineTransformScale( transform, 1.0, 1.2)
    tranform = CGAffineTransformTranslate( transform, 0, halfHeight)
    UIView.animateWithDuration(0.5)
    {
      button.transform = transform
    }
    

    上面的代码将按钮设置为 120% 高度,然后将其保留在那里。

    您可以使用 animateWithDuration 的较长变体之一,该变体带有使动画自动反转的选项。我把它留给你作为练习。

    编辑#2:

    我敲出一些代码来制作一个 3 步动画:

    func animateButton(step: Int)
    {
      let localStep = step - 1
      
      let halfHeight = aButton.bounds.height / 2
      
      var transform: CGAffineTransform
      switch step
      {
      case 2:
        //Make the center of the grow animation be the bottom center of the button
        transform = CGAffineTransformMakeTranslation(0, -halfHeight)
        
        //Animate the button to 120% of it's normal height.
        transform = CGAffineTransformScale( transform, 1.0, 1.2)
        transform = CGAffineTransformTranslate( transform, 0, halfHeight)
        UIView.animateWithDuration(0.5, animations:
          {
            aButton.transform = transform
          },
          completion:
          {
            (finshed) in
            animateButton(step)
        })
      case 1:
        //In the second step, shrink the height down to .25 of normal
        transform = CGAffineTransformMakeTranslation(0, -halfHeight)
        
        //Animate the button to 120% of it's normal height.
        transform = CGAffineTransformScale( transform, 1.0, 0.25)
        transform = CGAffineTransformTranslate( transform, 0, halfHeight)
        UIView.animateWithDuration(0.5, animations:
          {
            aButton.transform = transform
          },
          completion:
          {
            (finshed) in
            animateButton(step)
        })
      case 0:
        //in the final step, animate the button back to full height.
        UIView.animateWithDuration(0.5)
          {
            aButton.transform = CGAffineTransformIdentity
          }
      default:
        break
      }
    }
    

    你会调用它

    animateButton(3)
    

    使用枚举作为步数会更简洁,它可以使用一些范围检查来确保输入值是 3、2 或 1,但你明白了......

    【讨论】:

    • 感谢@DuncanC 的回复,您能否指出一些工作示例的方向,因为我不知道如何实现。谢谢
    • 谢谢@DuncanC,太完美了。我现在只是想使图像的高度增加并迅速降低到其高度的四分之一。是时候弄清楚了。再次感谢
    • 您可以使用具有完成方法的版本来嵌套动画,并让该方法调用另一个动画,将缩放变换动画降至 0.25。
    • 如果它满足您的需求,那么您应该接受它。如果你喜欢一个答案,一旦你有足够的声望就应该投票。
    • 抱歉询问@DuncanC,您提供的上述代码运行良好。我删除了我的代码,但是现在当我运行上面的代码时,按钮从底部而不是从顶部展开,有什么想法吗?
    猜你喜欢
    • 2013-04-30
    • 1970-01-01
    • 2022-12-16
    • 2017-11-24
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    相关资源
    最近更新 更多