ios - UIButton反转并发问题

标签 ios swift concurrency 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)
}

最佳答案

最简单的解决方案是在更改期间禁用按钮:

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

关于ios - UIButton反转并发问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34551168/

相关文章:

ios - 我会在预装应用程序上获得搜索广告归因数据吗?

concurrency - 如何使用 Condvar 和 Mutex 监控变化

ios - 核心数据 NSPredicate 使用 in 子句获取实体关系

ios - 将'NSString * __ strong'传递给不兼容类型'CLLocationDegrees'(又名'double')的参数

ios - Quickblox:体系结构 x86_64 的 undefined symbol :错误

ios - 在 Swift 中将参数传递给选择器

并行解析文件

c# - C#计时器2同时执行的 Action

ios - HealthKit 和 Today 扩展/小部件

ios - CGAffineTransform缩放和平移-动画前跳转