如何键值观察 CALayer 的旋转?
我可以像这样访问该值:
NSNumber* rotationZ = [myLayer valueForKeyPath:@"transform.rotation.z"];
但由于某种原因,如果我尝试 KV 观察该关键路径,则会收到编译器错误。 首先,这就是我尝试这样做的方式:
[myLayer addObserver:self forKeyPath:@"transform.rotation.z" options:0 context:nil];
编译器告诉我:
*** 由于未捕获的异常“NSUnknownKeyException”而终止应用程序, 原因: '[ addObserver:forKeyPath:@“rotation.z” 选项:0x0 上下文:0x528890] 已发送 到不符合 KVC 的对象 对于“旋转”属性。'
我不明白的是,为什么我可以通过 KVC 键路径访问该 z 值,但不能向其添加观察者。 这有道理吗?
我还能如何观察该矩阵的 z 值? 我不关心矩阵的其他值。 仅 Z 轴旋转。 还有其他方法可以访问和观察它吗?
I can access the value like this:
NSNumber* rotationZ = [myLayer valueForKeyPath:@"transform.rotation.z"];
But for some reason, if I try to KV-observe that key path, I get a compiler error. First, this is how I try to do it:
[myLayer addObserver:self forKeyPath:@"transform.rotation.z" options:0 context:nil];
The compiler tells me:
*** Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[
addObserver: forKeyPath:@"rotation.z"
options:0x0 context:0x528890] was sent
to an object that is not KVC-compliant
for the "rotation" property.'
what I don't get is, why I can access that z value by KVC key path, but not add an observer to it. Does this make sense?
How else could I observe the z value of that matrix? I don't care about the other values of the matrix. Only the z rotation. Any other way to access and observe it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CALayer
的transform
属性是一个结构体,而不是一个对象,因此它不符合 KVC。您应该能够做的是,绑定到变换属性并在收到 KVO 通知时拉出 Z 值,而不是绑定到 Z 旋转。
我认为这里的困惑在于,当您在 NSObject 上使用点表示法时,您实际上是在使用该对象的
- (id)property
和- (void)setProperty
方法,符合 KVC 标准。 当您在结构上使用点表示法时,您正在访问该结构的成员,而不是调用方法。The
transform
property for theCALayer
is a struct, not an object, so it isn't KVC compliant.What you should be able to do is, instead of binding to the Z rotation, bind to the transform property and pull the Z value out whenever you get the KVO notification.
I think the confusion here is that when you use dot notation on an NSObject, you're really using that object's
- (id)property
and- (void)setProperty
methods, which are KVC compliant. When you use dot notation on a struct, you're accessing a member of that struct, not calling a method.