Quartz 2D 在 alpha 褪色的 UIView 中绘制 RGB
我创建了一个自定义 UIView,它使用 Quartz 重写了 drawRect 方法。我最终设法让一切完全按照我想要的方式工作,但是有一种非常奇怪的行为,我无法理解。
如果我使用定义为 => 的自定义 UIView (@接口TagView:UIView) 将其 alpha 值设置为 1 然后它会完全按照我的预期进行绘制。
我遇到的问题是,在一个特定的实例中,我希望它以 0.3 的 alpha 值绘制,因此我将 alpha 值为 0.3 的视图添加到 IB 中。我使用 CGContextSetRGBStrokeColor 和 CGContextSetRGBFillColor 设置了不同的 RGB 值来绘制许多工件。这些都工作正常,直到我尝试为所有 3 个 RGB 值设置一个非零值。
为了说明我是否使用: CGContextSetRGBStrokeColor(上下文, 1, 1, 0, 1); 然后我得到一条黄线。 如果我使用: CGContextSetRGBStrokeColor(上下文, 1, 1, 0.001, 1); 我什么也没得到。
似乎只要我将其中一个 RGB 值设置为 0,它就会很高兴并进行绘制,但一旦所有三个值都非零,它就会失败。当视图的 alpha 设置为 1.0 时,这一切都工作正常(即我可以毫无问题地绘制白色)。
有人对可能导致此问题的原因或我如何诊断问题有任何想法或建议吗?
I've created a custom UIView which overrides the drawRect method using Quartz. I've eventually managed to get everything working exactly how I want it to however there is a really peculiar behaviour which I can't get my head around.
If I use the custom UIView defined as =>
(@interface TagView : UIView)
with it's alpha value set to 1 then it draws exactly as I would expect it to.
The problem I've come across is that in one particular instance I want it to draw at an alpha of 0.3 so I've added the view to IB with an alpha of 0.3. I draw a number of artifacts with different RGB values set using CGContextSetRGBStrokeColor and CGContextSetRGBFillColor. These all work fine until I try and set a non-zero value for all 3 RGB values.
To illustrate if I use:
CGContextSetRGBStrokeColor(context, 1, 1, 0, 1);
then I get a yellow line.
If I use:
CGContextSetRGBStrokeColor(context, 1, 1, 0.001, 1);
I get nothing at all.
It seems like as long as I set one of the RGB values to 0 it's happy and draws but as soon as all three values are non-zero it fails. Also this is all working fine when the alpha of the view is set to 1.0 (ie I can draw white without any issues whatsoever).
Does anyone have any thoughts or suggestions as to what might be causing this or how I might diagnose the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Garrrrrrr...看来我在论坛上发布问题30秒后自己解决的神秘能力又来了! (当然这是在拔掉头发 2 1/2 天之后......)。
看来我不小心将视图的背景颜色保留为白色。对于 0.3 alpha,实际上并不是很明显。白底白字==不可见,所以我怀疑这就是线条不可见的原因。我本以为几乎黄色会出现,但事实似乎并非如此。
Garrrrrrr... It seems my mystical ability to post a question on a forum ONLY to work it out myself 30 seconds later has struck again! (of course this is after 2 1/2 days of pulling hair out...).
It seems I had accidentally left the background colour of the view as white. With 0.3 alpha it wasn't actually very obvious. White on white == invisible so i suspect that's why the lines weren't visible. I would have thought that the ALMOST yellow would have shown up but that doesn't seem to be the case.
尝试将自定义视图的
opaque
属性设置为NO
。Try setting the
opaque
-property of your custom view toNO
.