在我的應用程序中,我按下了按鈕,它使用iOS默認按鈕動畫更改了backgroundcolor和類似的數字。按鈕的默認行爲就像數字只能位於區間[x,x + 1]或[x-1,x]中,其中x是初始值。但是,如果按鈕被快速按下,相似的數字就會迅速增加或減少。當按下太快時,UIButton sender.titleLabel?.text?.toInt()不會提供更新值
func likeButtonAction(sender:UIButton!) {
var oldValue = sender.titleLabel?.text?.toInt()
println("oldvalue \(oldValue)")
if sender.selected {
//upvote
sender.setTitle(String(oldValue! + 1), forState: UIControlState.Normal|UIControlState.Selected)
println("inc \(oldValue! + 1)")
} else {
//downvote
sender.setTitle(String(oldValue! - 1), forState: UIControlState.Normal|UIControlState.Selected)
println("dec \(oldValue! - 1)")
}
}
EDIT1: 當按下迅速輸出:
oldvalue Optional(3)
inc 4
oldvalue Optional(4)
dec 3
oldvalue Optional(3)
inc 4
oldvalue Optional(4)
dec 3
oldvalue Optional(4)
inc 5
oldvalue Optional(5)
dec 4
oldvalue Optional(5)
inc 6
oldvalue Optional(6)
dec 5
oldvalue Optional(6)
inc 7
oldvalue Optional(7)
dec 6
oldvalue Optional(7)
inc 8
oldvalue Optional(8)
dec 7
oldvalue Optional(8)
inc 9
oldvalue Optional(9)
dec 8
oldvalue Optional(8)
inc 9
EDIT2: SOLUTION: 這個工作正常,但我不知道爲什麼。解釋將不勝感激
func likeButtonAction(sender:UIButton!)
if sender.selected {
//upvote
sender.setTitle(String((sender.titleLabel?.text?.toInt())! + 1), forState: UIControlState.Normal|UIControlState.Selected)
} else {
//downvote
sender.setTitle(String((sender.titleLabel?.text?.toInt())! - 1), forState: UIControlState.Normal|UIControlState.Selected)
}
}
改變了這種方式,但問題仍然存在。所以我的假設是錯誤的我會編輯 – Thellimist 2015-02-06 15:18:27