菜鸟教程小白 发表于 2022-12-12 11:04:12

ios - 是否可以继承 CAAnimation?


                                            <p><p>我只想通过中心点 C 和半径 R 的圆弧从 A 点到 B 点对图层的 <code>position</code> 属性进行动画处理。如此简单,但我觉得我正在尝试弯曲 Core Animation一半来做到这一点。</p>

<p>似乎理想的方法是简单地创建一个自定义 <code>CAPropertyAnimation</code> 子类,例如:</p>

<pre><code>let anim = MyArcPropertyAnimation(keyPath: &#34;position&#34;, center: CGPoint, radius: CGFloat)
anim.fromValue = fromPoint
anim.toValue = toPoint
myLayer.addAnimation(anim, forKey: &#34;positionArcAnimation&#34;)
</code></pre>

<p>但我找不到任何关于子类化 <code>CAAnimation</code> 或其任何派生类的信息。 <a href="https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CAValueFunction_class/index.html#//apple_ref/swift/cl/c:objc(cs)CAValueFunction" rel="noreferrer noopener nofollow"><code>CAValueFunction</code></a> 似乎也是如此这似乎也很有希望。</p>

<p>注意:我使用 <a href="https://github.com/facebook/pop" rel="noreferrer noopener nofollow">POP</a> 运行它很容易,但发现,<a href="https://medium.com/@beefon/should-you-use-pop-b986b10d4079#.5ag73ltzt" rel="noreferrer noopener nofollow">as warned by a facebook engineer</a> ,性能对我来说太慢了。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>文档似乎暗示 <code>CAAnimation</code> 不是为子类化而设计的。此外,大多数动画都是在窗口服务器(称为 <code>backboardd</code>)中执行的,您肯定无法让 <code>backboardd</code> 来执行您的代码。</p>

<p>不管怎样,听起来你可以使用 <code>CAKeyframeAnimation</code> 获得你想要的效果。将 <code>CAKeyframeAnimation</code> 的 <code>path</code> 属性设置为您希望图层遵循的弧线。 Playground 示例:</p>

<pre><code>import UIKit
import XCPlayground

let view = UIView(frame: CGRectMake(0, 0, 400, 400))
view.backgroundColor = .whiteColor()
let mover = UIView(frame: CGRectMake(0, 0, 10, 10))
mover.backgroundColor = .redColor()
view.addSubview(mover)
XCPlaygroundPage.currentPage.liveView = view

let animation = CAKeyframeAnimation(keyPath: &#34;position&#34;)
let path = UIBezierPath(arcCenter: CGPointMake(200, 200), radius: 160, startAngle: 1, endAngle: 5, clockwise: true)
animation.path = path.CGPath
animation.duration = 2
animation.calculationMode = kCAAnimationPaced
mover.layer.addAnimation(animation, forKey: &#34;position&#34;)
mover.center = path.currentPoint
</code></pre>

<p>结果:</p>

<p> <a href="/image/gDosx.gif" rel="noreferrer noopener nofollow"><img src="/image/gDosx.gif" alt="playground demo"/></a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 是否可以继承 CAAnimation?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/36855798/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/36855798/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 是否可以继承 CAAnimation?