基本上,我要做的就是将layer.fillColor
和CGColor
进行比较。
现在,在Swift 4中不推荐使用UIColor.black.cgColor
函数。
我努力了:
if(layer.fillColor === UIColor.black.cgColor){
return
}
而且仍然不起作用。我猜他们必须具有相同的kCGColorSpaceModel。
这是日志中每种颜色的输出
<CGColor 0x1c02a15c0> [<CGColorSpace 0x1c02a0a20> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)] ( 0 0 0 1 )
<CGColor 0x1c008e290> [<CGColorSpace 0x1c02a0f60> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 0 1 )
有什么解决办法?
最佳答案
这是CGColor
的扩展,用于检查给定的颜色是否为黑色。这适用于RGB和灰度颜色空间中的颜色。
extension CGColor {
func isBlack() -> Bool {
let count = numberOfComponents
if count > 1 {
if let components = components {
for c in 0..<components.count-1 { // skip the alpha component
// All components are 0 for black
if components[c] != 0.0 {
return false
}
}
return true
}
}
return false
}
}
print(UIColor.black.cgColor.isBlack())
print(UIColor(red: 0, green: 0, blue: 0, alpha: 1).cgColor.isBlack())
您可以将其用作:
if layer.fillColor.isBlack() {
return
}