是否可以继承 CAAnimation?

Is it possible to subclass CAAnimation?

我只想为图层的 position 属性 通过中心点 C 和半径 R 的圆弧从点 A 到点 B 设置动画。如此简单,但我觉得我正在尝试将 Core Animation 对折以执行此操作。

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

let anim = MyArcPropertyAnimation(keyPath: "position", center: CGPoint, radius: CGFloat)
anim.fromValue = fromPoint
anim.toValue = toPoint
myLayer.addAnimation(anim, forKey: "positionArcAnimation")

但是我找不到关于子类化 CAAnimation 或其任何派生类的任何信息。 CAValueFunction 似乎也是如此,否则似乎也很有希望。

注意:我使用 POP easily enough but found that, as warned by a facebook engineer 得到它 运行,性能对我的使用来说太慢了。

文档中似乎暗示 CAAnimation 不是为子类化而设计的。此外,大多数动画都是在 window 服务器(称为 backboardd)中执行的,您肯定无法让 backboardd 执行您的代码。

无论如何,听起来您可以使用 CAKeyframeAnimation 获得您想要的效果。将 CAKeyframeAnimationpath 属性 设置为您希望图层遵循的弧线。游乐场示例:

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: "position")
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: "position")
mover.center = path.currentPoint

结果: