ios - 选择时更改自定义 UIView 背景

标签 ios objective-c uiview uicolor

我创建了三个自定义圆 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/

相关文章:

ios - 在 Swift 中导入扩展文件

ios - "Cannot invoke ' CGPointMake ' with an argument list of type ' ((), CGFloat) '"在 Swift 中收到

ios - 用于在 iPhone 设备上显示持久存储内容的 GUI

objective-c - NSScrollView 重叠文本

iOS:当应用程序处于前台时,如何让用户忽略远程通知?

iphone - 为什么这个动画选择器不会被调用?

ios - Swift 2.1 与 removeFromSuperview 相反

iphone - 在 NSMutableArray 中添加 C++ 对象

ios - swift : Cannot validate password

iphone - 奇怪的 RestKit/RKRequestQueue 同步问题