59.消除设置按扭标题的闪动

2019-03-25  本文已影响0人  noonez
import UIKit

/*
 两种方法消除按钮设置title时会闪动的问题:
 1.将按钮的buttonType初始化为.custom
 2.在调用button.setTitle("\(count)", for: .normal)前,先设置button.titleLabel?.text = "\(count)"
 */
class ViewController: UIViewController {

    //button1我已在storyboard中将其buttonType设置为custom
    @IBOutlet weak var button1: UIButton!
    
    @IBOutlet weak var button2: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        timerStart()
    }
    
    func timerStart() {
        var count = 0
        if #available(iOS 10.0, *) {
            Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (timer) in
                self.button1.setTitle("\(count)", for: .normal)
                
                self.button2.titleLabel?.text = "\(count)"
                self.button2.setTitle("\(count)", for: .normal)
                
                count += 1
            }
        } else {
            // Fallback on earlier versions
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读