美文网首页
59.消除设置按扭标题的闪动

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

作者: noonez | 来源:发表于2019-03-25 16:12 被阅读0次
    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
            }
        }
    }
    

    相关文章

      网友评论

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

          本文链接:https://www.haomeiwen.com/subject/fnkbvqtx.html