【问题标题】:UIButton animation title position changed unexpectedUIButton 动画标题位置意外更改
【发布时间】:2018-03-28 01:16:10
【问题描述】:

UIButton 动画标题位置突然改变(向左跳),我想将标题始终保持在附近的按钮中心。

可以看到按钮标题(“登录”)意外跳到左边,应该优化一下。

代码如下:

UIView.animate(withDuration: 1.5, delay: 0.0, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: [], animations: {
        self.loginButton.bounds.size.width -= 80.0
        }){ (yes) in }
 UIView.animate(withDuration: 0.33, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.0, options: [], animations: {
        self.loginButton.center.y -= 60.0
        self.loginButton.backgroundColor = UIColor(red: 0.63, green: 0.84, blue: 0.35, alpha: 1.0)
        self.spinner.center = CGPoint(x: -20, y: -16)
        self.spinner.alpha = 0.0
  }){ (yes) in  }

这是我试过的代码,它也不起作用。

UIView.animate(withDuration: 0.3, delay: 0.2, options: [], animations: {

       self.loginButton.backgroundColor = UIColor(red: 0.63, green: 0.84, blue: 0.35, alpha: 1.0)
       self.loginButton.bounds.size.width -= 80.0
       self.loginButton.center.y -= 60.0
       self.spinner.frame.origin = CGPoint(x: -20, y: 16)
       self.spinner.alpha = 0.0
 }) { (isOK) in        }

PS:

我通过storyboard添加登录按钮,如下图

相关代码是here

【问题讨论】:

  • 如何添加登录按钮?上传添加登录按钮的代码
  • 信息更新@JD。

标签: animation uibutton swift4


【解决方案1】:

在动画函数中添加线条

UIView.animate(withDuration: 1, delay: 0.2, options: [], animations: {

    //Your code for animation
    .    
    .
    .


    //Add this line
    self.view.layoutIfNeeded()

}) { (isOK) in        }

注意

使用 自动布局 支持所有设备并轻松制作动画

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    • 2018-01-27
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多