我正在使用 Opacity 生成所有 Quartz2D 图稿,现在正在解决通过 KVC/KVO 更改颜色的问题。 Opacity 将其所有颜色变量定义为 @dynamic 并实现其自己的访问器作为类定义的一部分。
我的问题是;如何通过键值传递新的 CGColorRef 值?
到目前为止(作为测试平台)我已经做到了这一点:
// Testbed ... Testbed ... Testbed ... Testbed ... Testbed ... Testbed ... Testbed ...
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGFloat components[4] = {1.0f, 1.0f, 1.0f, 1.0f};
CGColorRef color = CGColorCreate(space, components);
[myCALayerReceiverObject setValue: color forKey: @"numColour"];
// Testbed ... Testbed ... Testbed ... Testbed ... Testbed ... Testbed ... Testbed ...
颜色空间和创建代码直接从 Opacity 的 CALayer 类生成器中提取(我还没有那么先进),但传递“颜色”作为值会导致 iPhone 模拟器崩溃并且 XCode 给了我一个非常神秘的警告;
“不兼容的指针类型将‘CGColorRef’(又名结构‘CGColor *’)发送到‘id’类型的参数”。
如何包装 CGColorRef 以将其通过 KVC 传递到接收器?
提前致谢。
I'm using Opacity to generate all my Quartz2D artwork and I'm now tackling the issue of changing colours via KVC/KVO. Opacity defines all its colour variables as @dynamic and implements its own accessors as part of the class definition.
My question is; how do I pass a new CGColorRef value by key value?
So far (as a testbed) I've got this far:
// Testbed ... Testbed ... Testbed ... Testbed ... Testbed ... Testbed ... Testbed ...
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGFloat components[4] = {1.0f, 1.0f, 1.0f, 1.0f};
CGColorRef color = CGColorCreate(space, components);
[myCALayerReceiverObject setValue: color forKey: @"numColour"];
// Testbed ... Testbed ... Testbed ... Testbed ... Testbed ... Testbed ... Testbed ...
The colour space and creation code is lifted directly from Opacity's CALayer class generator (I'm not that advanced yet) but passing 'color' as the value causes the iPhone simulator to crash and XCode gives me a very cryptic warning;
"Incompatible pointer types sending 'CGColorRef' (aka struct 'CGColor *') to parameter of type 'id' ".
How do I wrap up the CGColorRef to pass it across KVC to the receiver?
Thanks in advance.
发布评论
评论(3)
将 CGColorRef 转换为 (id) 有效。下面是将阴影设置为黑色的示例:
Casting the CGColorRef to (id) works. Here is an example setting the shadow to the color black:
我建议使用 NSValue 来包装您的颜色对象,但在测试过程中我得出的结论是 KVO/KVC 不适用于 CGColors。根据键值编码编程指南,原始类型和结构体使用 NSValue 或 NSNumber 进行包装。但是,当尝试访问 CGColorRef 属性时,我收到“不符合 KVC”异常。似乎指向结构的指针没有像结构本身一样包装到 NSValues 中。这是我的测试代码:
一种可能的解决方法是向适当的类添加一个类别,该类别定义不同键的访问器。这些访问器将是
numColour
键的 NSValue 包装器。然后您将在测试台中设置颜色,如下所示:
I was going to suggest using NSValue to wrap your color object, but during testing I have come to the conclusion that KVO/KVC will not work with CGColors. According to the Key-Value Coding Programming Guide, primitive types and structures are wrapped using NSValue or NSNumber. However, when trying to access a
CGColorRef
property, I get a "not KVC compliant" exception. It seems that pointers to structures are not wrapped to NSValues in the same way as the structures themselves. Here is my test code:One possible workaround is to add a category to the proper class which defines accessors for a different key. These accessors would be NSValue wrappers for the
numColour
key.Then you would set the color in your testbed as follows:
该错误是相当不言自明的 - 您需要将
CGColorRef
转换为(id)
。在我的脑海中,这应该有效:The error is fairly self-explanatory - you need to cast your
CGColorRef
as(id)
. Off the top of my head, this should work: