2015-12-31 44 views
0

我有一個關於反轉uibutton顏色的問題。我試圖改變文字顏色和背景顏色;然而,當用戶試圖非常快地按下時,顏色停止反轉。我相信這是一個併發問題。 我嘗試過使用鎖和操作隊列。任何人有任何建議?UIButton反轉併發問題

繼承人我的代碼

func invertStartButton(){ 
    let currentBackGroundColor:UIColor? = self.startButton.backgroundColor 
    print(currentBackGroundColor) 
    objc_sync_enter(currentBackGroundColor) 
    defer{ 
     objc_sync_exit(currentBackGroundColor) 
    } 
    self.startButton.backgroundColor = self.startButton.titleLabel?.textColor 
    print(self.startButton.backgroundColor) 

    self.startButton.setTitleColor(currentBackGroundColor, forState: UIControlState.Normal) 
} 

回答

0

最簡單的解決方法是禁用改變按鈕期間:

@IBAction func invertButton(sender: UIButton) { 
    sender.enabled = false 
    // invert 
    sender.enabled = true 
} 
+0

我試過這個解決方案,但似乎仍然有相同的結果 – matt

+0

這實際上是不可能的。 – Mundi

+0

它有一些事情要做這行讓currentBackGroundColor:UIColor? = self.startButton.backgroundColor,所以我決定只是硬編碼的顏色,並有一個布爾值來解決反轉。這個答案不起作用,因爲它不是一個按鈕問題,而是更多的設置顏色問題 – matt

0

問題曾與實例變量做。所以,爲了解決這個問題,我使用了一個具有bool值的兩個硬編碼代碼用於切換