UIButton动画标题位置意外改变

时间:2018-03-28 01:16:10

标签: animation uibutton swift4

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

image description

您可以看到按钮标题(“登录”)跳转到左侧意外,应该进行优化。

以下是代码:

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:

我通过故事板添加登录按钮,如下图所示

 image description

相关代码为here

1 个答案:

答案 0 :(得分:1)

在动画功能中添加线

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

    //Your code for animation
    .    
    .
    .


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

}) { (isOK) in        }

注意

使用自动布局获取所有设备支持和易于动画