我正在使用 aUIViewPropertyAnimator
为视图的位置设置动画。但是,我还想为 CALayer 属性设置动画borderColor
。目前,它没有动画并在开始时立即更改,如下所示:
这是我的代码:
class ViewController: UIViewController {
var animator: UIViewPropertyAnimator?
let squareView = UIView(frame: CGRect(x: 0, y: 40, width: 80, height: 80))
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(squareView)
squareView.backgroundColor = UIColor.blue
squareView.layer.borderColor = UIColor.green.cgColor
squareView.layer.borderWidth = 6
animator = UIViewPropertyAnimator(duration: 2, curve: .easeOut, animations: {
self.squareView.frame.origin.x = 100
self.squareView.layer.borderColor = UIColor.red.cgColor
})
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.animator?.startAnimation()
}
}
}
我查看了这个问题,How to synchronously animate a UIView and a CALayer,答案建议使用“显式动画,如基本动画”。它说,
如果两个动画的时间函数和持续时间相同,那么它们应该保持对齐。
但是,如果我使用CABasicAnimation
,我将失去 的所有好处UIViewPropertyAnimator
,例如计时和中途停止。我还需要跟踪两者。有没有办法用 动画CALayer
属性UIViewPropertyAnimator
?