我创建了三个自定义圆 UIViews
。当用户单击其中一个时,我想将 View 的背景颜色更改为灰色。
最初创建 View 时,它是一个随机颜色。如果再次选择 View ,我希望这种颜色保持不变。 IE。再次选择时从彩色变为灰色,然后从灰色变为彩色。
更改 View 背景或以某种方式向我的自定义 UIView 添加叠加层以使其变灰的最佳方法是什么?
最佳答案
在“选中”时更改 View 背景的一种方法是拥有 View 的子类并添加触摸事件。
class CustomView: UIView {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.backgroundColor = UIColor.selected
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
self.backgroundColor = UIColor.default
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
self.backgroundColor = UIColor.default
}
}
关于ios - 选择时更改自定义 UIView 背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23521912/