iOS Swift UI 更新延迟

标签 ios swift user-interface swift4

我有一个 UIView 的子类。在本类(class)中,我想在更改特定 bool 标志时更改背景。 (我通过 touchesBegan 控制这个标志)

下面是我的代码。

class TileView: UIView {

    let selectedBackgroundColor: UIColor = .cyan
    let nonSelectedBackgroundColor: UIColor = .white

    private var isSelected = false {
        didSet {
            backgroundColor = isSelected ? self.selectedBackgroundColor : self.nonSelectedBackgroundColor
            print("isSelected: " + String(isSelected))
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
       isSelected = !isSelected
    }
}

touchesBegan 被触发时,我在控制台中得到 isSelected: true 但背景颜色没有改变。

我注意到,在 View 中点击后,如果我点击 View 之外屏幕上的其他位置,它会改变颜色。

我很困惑到底是什么原因造成的。

编辑:

这里是行为的简短记录,因为我的描述不够清楚。 录制中的每个白色方 block 都是一个 TileView。 https://gyazo.com/e8152d18b4efe1c5b1c6c04b55b661fe

最佳答案

所以我发现我的代码没问题,问题实际上是模拟器没有正确更新。 有关更多详细信息,请参阅此处的答案。

iOS Simulator does not refresh correctly

关于iOS Swift UI 更新延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59595944/

相关文章:

swift - 仅从模块导入特定扩展

ios - 为什么 renderInContext 比绘制到屏幕慢得多?

ios - 如何修复 Facebook SDK "does not contain bitcode"错误?

objective-c - 每个 .swift 文件不应该是一个类吗?

swift - 如何为单个段设置 UISegmentedControl Tint Color

windows - 无法在 MySQL 8 中使用 DBeaver 7 创建新列、约束、索引

c# - 使用 C# API 进行 iOS 开发

ios - 如何在 ios 中的 UIViewController 中设置背景图像中的图像

java - IntelliJ IDEA 无法创建 GUI 表单

java - SWT:定期更新GUI