为什么 `transform.rotation` 可用于 CoreAnimation 但不能使用 getter 访问?
Why is `transform.rotation` available for CoreAnimation but not accessible using a getter?
我看到很多旋转视图的代码如下:
CABasicAnimation *centerToRightRotate
= [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
centerToRightRotate.fromValue = @(0);
centerToRightRotate.toValue = @(M_PI/8);
centerToRightRotate.duration = 0.15;
[self.view.layer addAnimation:centerToRightRotate forKey:nil];
(例如,的许多答案)
但是,当我尝试访问 self.view.layer.transform.rotation
或 self.view.layer.transform.rotation.z
时,编译器会告诉我 "No member named 'rotation' in CATransform3D"。 docs for CATransform3D
也不显示 rotation
作为实例 属性。
我的猜测是 CAAnimation
以某种方式将 transform.rotation
关键路径转换为适当的转换,但我想知道幕后实际发生了什么。这到底是怎么回事?
Core Animation extends the NSKeyValueCoding protocol as it pertains to the CAAnimation and CALayer classes. This extension adds default values for some keys, expands wrapping conventions, and adds key path support for CGPoint, CGRect, CGSize, and CATransform3D types.
rotation
不是 CATransform3D
的 属性。支持关键路径以方便地指定要设置动画的数据结构字段。
You can also use these conventions in conjunction with the setValue:forKeyPath: and valueForKeyPath: methods to set and get those fields.
Setting values using key paths is not the same as setting them using Objective-C properties. You cannot use property notation to set transform values. You must use the setValue:forKeyPath: method with the preceding key path strings.
可以使用setValue:forKeyPath:
、valueForKeyPath:
设置或获取,但不能使用属性表示法。
如果您想知道引擎盖下到底发生了什么,请了解有关 NSKeyValueCoding
here
的更多信息
我看到很多旋转视图的代码如下:
CABasicAnimation *centerToRightRotate
= [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
centerToRightRotate.fromValue = @(0);
centerToRightRotate.toValue = @(M_PI/8);
centerToRightRotate.duration = 0.15;
[self.view.layer addAnimation:centerToRightRotate forKey:nil];
(例如,
但是,当我尝试访问 self.view.layer.transform.rotation
或 self.view.layer.transform.rotation.z
时,编译器会告诉我 "No member named 'rotation' in CATransform3D"。 docs for CATransform3D
也不显示 rotation
作为实例 属性。
我的猜测是 CAAnimation
以某种方式将 transform.rotation
关键路径转换为适当的转换,但我想知道幕后实际发生了什么。这到底是怎么回事?
Core Animation extends the NSKeyValueCoding protocol as it pertains to the CAAnimation and CALayer classes. This extension adds default values for some keys, expands wrapping conventions, and adds key path support for CGPoint, CGRect, CGSize, and CATransform3D types.
rotation
不是 CATransform3D
的 属性。支持关键路径以方便地指定要设置动画的数据结构字段。
You can also use these conventions in conjunction with the setValue:forKeyPath: and valueForKeyPath: methods to set and get those fields.
Setting values using key paths is not the same as setting them using Objective-C properties. You cannot use property notation to set transform values. You must use the setValue:forKeyPath: method with the preceding key path strings.
可以使用setValue:forKeyPath:
、valueForKeyPath:
设置或获取,但不能使用属性表示法。
如果您想知道引擎盖下到底发生了什么,请了解有关 NSKeyValueCoding
here