代码之家  ›  专栏  ›  技术社区  ›  peco

为什么coreanimation可以使用“transform.rotation”,但不能使用getter访问?

  •  2
  • peco  · 技术社区  · 6 年前

    我看到很多代码可以按如下方式旋转视图:

    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.rotation self.view.layer.transform.rotation.z ,编译器将告诉我“catransform3d中没有名为‘rotation’的成员”。这个 docs for CATransform3D 也不显示 rotation 作为实例属性。

    我猜是 CAAnimation 以某种方式翻译 transform.rotation 进入适当转变的关键路径,但我想知道在幕后到底发生了什么。这到底是怎么回事?

    1 回复  |  直到 6 年前
        1
  •  3
  •   trungduc 邱林和    6 年前

    根据 Key-Value Coding Extensions

    核心动画扩展了nskeyvaluecoding协议,因为它属于caanimation和calayer类。此扩展为某些键添加默认值,扩展包装约定,并为cgpoint、cgrect、cgsize和catransform3d类型添加键路径支持。

    rotation 不是的财产 CATransform3D .支持键路径来指定要以方便的方式设置动画的数据结构的字段。

    还可以将这些约定与setValue:forkeyPath:和valueForkeyPath:方法结合使用来设置和获取这些字段。

    使用键路径设置值与使用objective-c属性设置值不同。不能使用属性表示法设置转换值。必须将setValue:forkeyPath:方法与前面的键路径字符串一起使用。

    你可以用 setValue:forKeyPath: 我是说, valueForKeyPath: 设置或获取,但不能使用属性表示法。

    如果你想知道引擎盖下到底发生了什么,就要了解更多 NSKeyValueCoding here