Animation UIButton动画标题位置意外更改

Animation UIButton动画标题位置意外更改,animation,uibutton,swift4,Animation,Uibutton,Swift4,UIButton动画标题位置突然改变(跳到左侧),我希望标题始终保持在按钮中心附近 您可以看到按钮标题(“登录”)跳转到左侧,这是意外的,应该进行优化 代码如下: UIView.animate(withDuration: 1.5, delay: 0.0, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: [], animations: { self.loginButton.bounds.size.

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        }

附言:

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


与此相关的代码是

在动画功能中添加行

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

    //Your code for animation
    .    
    .
    .


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

}) { (isOK) in        }
注意


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

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