Reputation: 2890
I am following the answer shown here in stackoverflow to implement a countdown timer. The timer text does not flash (works perfectly) when the text is set for a label.
When the same text is set for a button it flashes every time the title is set. How can avoid the button text flashing ?
@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
}
}
Upvotes: 2
Views: 1836
Reputation: 267
flashes because it gets hit everytime you -1
try this one
@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)
}
Upvotes: 0
Reputation: 148
This is caused through the setting of the button title text. Just set the type of the button to custom and the flashing should stop
Upvotes: 11