我按照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
}
}
答案 0 :(得分:7)
这是通过设置按钮标题文本引起的。只需将按钮的类型设置为自定义,闪烁应停止
答案 1 :(得分:0)
闪烁,因为每当你-1
尝试这个时,它就会被击中
@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
count -= 1
}
countDownTimerButton.setTitle(text, for: .normal) // Setting text for button, (Text flashes everytime it is set)
}