簡體   English   中英

UIButton反向並發問題

[英]UIButton Inversion Concurrency issue

我有一個關於反轉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)
}

最簡單的解決方案是在更改過程中禁用按鈕:

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

問題與實例變量有關。 因此,為了解決此問題,我使用了兩個帶有布爾值的硬編碼代碼進行切換

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM