gpt4 book ai didi

ios - 如何检查 UIView 的背景颜色是否为 clearColor?

转载 作者:行者123 更新时间:2023-12-01 17:38:26 24 4
gpt4 key购买 nike

这行不通:

if([myView.backgroundColor isEqual:[UIColor clearColor]])
NSLog("Background is clear");
else
NSLog("Background is not clear");

P.S:要重现案例,请在界面生成器中拖动一个 uiview,将其背景颜色设置为从界面生成器中清除颜色。设置 View 的导出,然后使用上面的代码在 viewDidLoad 中进行比较。

这里是测试项目的链接:https://drive.google.com/file/d/0B_1hGRxJtrLjMzUyRHZyeV9SYzQ/view?usp=sharing

这是 Interface Builder 的快照: enter image description here

最佳答案

也许两种颜色都清晰但仍然不一样?仅仅 alpha 值相同是不够的...

UIColor.clearColor() == UIColor(white: 1.0, alpha: 0.0) // false
UIColor.clearColor() == UIColor(white: 0.0, alpha: 0.0) // true

尝试

if([myView.backgroundColor isEqual:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0]])
NSLog("Background is clear");
else
NSLog("Background is not clear");

更好的方法可能是只检查 alpha 是否为 0.0:

CGFloat alpha = CGColorGetAlpha(myView.backgroundColor.CGColor);
if( alpha == 0.0 )
NSLog("Background is clear");
else
NSLog("Background is not clear");

关于ios - 如何检查 UIView 的背景颜色是否为 clearColor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31565581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com