使用计时器功能Swift设置标题时,按钮文本会闪烁

use*_*776 2 uibutton uilabel ios swift

我按照stackoverflow中显示的答案来实现倒数计时器.为标签设置文本时,计时器文本不会闪烁(完美地工作).

当为按钮设置相同的文本时,每次设置标题时它都会闪烁.如何避免按钮文字闪烁?

@IBOutlet weak var countDownTimer: UILabel!
@IBOutlet weak var countDownTimerButton: UIButton!
var count = 120

override func viewDidLoad() {
    super.viewDidLoad()

    var _ = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(update), userInfo: nil, repeats: true)
    // Do any additional setup after loading the view.
}


func update() {

    if(count > 0){
        let minutes = String(count / 60)
        let seconds = String(count % 60)
        countDownTimer.text = minutes + ":" + seconds // Setting text for label (Works perfectly)

        let text = minutes + ":" + seconds
        countDownTimerButton.setTitle(text, for: .normal) // Setting text for button, (Text flashes everytime it is set)
        count -= 1
    }

}
Run Code Online (Sandbox Code Playgroud)

小智 7

这是通过设置按钮标题文本引起的.只需将按钮类型设置为自定义,闪烁就应该停止