当按下按钮但卡住时,尝试在UIView.animate中更改按钮标题和label.center.x.为什么? Swift 4 Xcode 9.4

问题描述 投票:0回答:2

我的问题

我正在尝试使用UIView.animate(..)在touchInside按钮时使用标签做一些动画。一切都还行,直到我添加一行:“self?.confirm.setTitle(”Đăngnhập“,for:。normal)。动画不起作用。

我的意志

我想将Register下面的黄色下划线切换到下面的Login。

代码是好的

@IBAction func signUpAction(_ sender:Any?){
    if (signup == false){
        signup = true
        UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseIn], animations: {[weak self] in
            self?.underlineSignup.center.x -= (self?.underlineSignup.bounds.width)!
           self?.view.layoutIfNeeded()
        }, completion: nil)
    }
}
@IBAction func signInAction(_ sender:Any?){
    if (signup == true){
        signup = false
        UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseIn], animations: {[weak self] in
            self?.underlineSignup.center.x += (self?.underlineSignup.bounds.width)!
            self?.view.layoutIfNeeded()
        }, completion: nil)
    }
}

It work

但是当我添加.setTitle时

@IBAction func signUpAction(_ sender:Any?){
    if (signup == false){
        signup = true
        UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseIn], animations: {[weak self] in
            self?.underlineSignup.center.x -= (self?.underlineSignup.bounds.width)!
            self?.confirm.setTitle("Đăng ký", for: .normal)
            self?.view.layoutIfNeeded()
        }, completion: nil)
    }
}
@IBAction func signInAction(_ sender:Any?){
    if (signup == true){
        signup = false
        UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseIn], animations: {[weak self] in
            self?.underlineSignup.center.x += (self?.underlineSignup.bounds.width)!
            self?.confirm.setTitle("Đăng nhập", for: .normal)
            self?.view.layoutIfNeeded()
        }, completion: nil)
    }
}

It stuck, only the title of button confirm change, the underline doesn't move

请任何人都可以解释这种情况。

编辑:

动画工作,但它的目的地始终是第一个Đăngký(动画来自左侧或右侧,结果总是第一位)

swift button label xcode9.4
2个回答
0
投票

我想我知道它是怎么回事。因为在setTitle中有updateFrame Constraintlayout的视图。构建按钮时我有一个contraintlayout。当我设置标题时,它使用约束重置视图。这就是我对此的看法。


0
投票

尝试将'setTitle'完成。

UIView.animate(withDuration: 0.15, delay: 0, options: .curveLinear, animations: {

       self?.underlineSignup.center.x -= (self?.underlineSignup.bounds.width)!
       self?.view.layoutIfNeeded()

        }){

               //completion
             self?.confirm.setTitle("Đăng ký", for: .normal)


        }
© www.soinside.com 2019 - 2024. All rights reserved.