菜鸟教程小白 发表于 2022-12-12 14:45:28

ios - 为什么 `transform.rotation` 可用于 CoreAnimation 但无法使用 getter 访问?


                                            <p><p>我看到很多旋转 View 的代码如下:</p>

<pre><code>CABasicAnimation *centerToRightRotate
    = ;
centerToRightRotate.fromValue = @(0);
centerToRightRotate.toValue = @(M_PI/8);
centerToRightRotate.duration = 0.15;

;
</code></pre>

<p>(例如,<a href="https://stackoverflow.com/questions/27755504/rotate-a-view-for-360-degrees-indefinitely-in-swift" rel="noreferrer noopener nofollow">this question</a> 的许多答案)</p>

<p>但是,当我尝试访问 <code>self.view.layer.transform.rotation</code> 或 <code>self.view.layer.transform.rotation.z</code> 时,编译器会告诉我“在 CATransform3D 中没有名为‘旋转’的成员”。 <a href="https://developer.apple.com/documentation/quartzcore/catransform3d" rel="noreferrer noopener nofollow">docs for <code>CATransform3D</code></a>也不将 <code>rotation</code> 显示为实例属性。</p>

<p>我的猜测是 <code>CAAnimation</code> 以某种方式将 <code>transform.rotation</code> 关键路径转换为适当的转换,但我想知道幕后实际发生了什么。这里到底发生了什么?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>根据<a href="https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreAnimation_guide/Key-ValueCodingExtensions/Key-ValueCodingExtensions.html" rel="noreferrer noopener nofollow">Key-Value Coding Extensions</a> </p>
<blockquote>
<p>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.</p>
</blockquote>
<p><code>rotation</code> 不是 <code>CATransform3D</code> 的属性。支持键路径来指定数据结构的字段,以方便地进行动画处理。</p>
<blockquote>
<p>You can also use these conventions in conjunction with the setValue:forKeyPath: and valueForKeyPath: methods to set and get those fields.</p>
<p>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.</p>
</blockquote>
<p>您可以使用<code>setValue:forKeyPath:</code>, <code>valueForKeyPath:</code> 来设置或获取它,但不能使用属性表示法。</p>
<p>如果您想了解幕后的实际情况,请了解有关 <code>NSKeyValueCoding</code> <a href="https://www.cocoawithlove.com/2010/01/5-key-value-coding-approaches-in-cocoa.html" rel="noreferrer noopener nofollow">here</a> 的更多信息</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 为什么`transform.rotation` 可用于 CoreAnimation 但无法使用 getter 访问?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/51146584/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/51146584/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 为什么 `transform.rotation` 可用于 CoreAnimation 但无法使用 getter 访问?