OGeek|极客世界-中国程序员成长平台

标题: ios - 为什么 `transform.rotation` 可用于 CoreAnimation 但无法使用 getter 访问? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:45
标题: ios - 为什么 `transform.rotation` 可用于 CoreAnimation 但无法使用 getter 访问?

我看到很多旋转 View 的代码如下:

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];

(例如,this question 的许多答案)

但是,当我尝试访问 self.view.layer.transform.rotationself.view.layer.transform.rotation.z 时,编译器会告诉我“在 CATransform3D 中没有名为‘旋转’的成员”。 docs for CATransform3D也不将 rotation 显示为实例属性。

我的猜测是 CAAnimation 以某种方式将 transform.rotation 关键路径转换为适当的转换,但我想知道幕后实际发生了什么。这里到底发生了什么?



Best Answer-推荐答案


根据Key-Value Coding Extensions

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 的更多信息

关于ios - 为什么 `transform.rotation` 可用于 CoreAnimation 但无法使用 getter 访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51146584/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4