我有一个CAShapeLayer
并试图为几个属性设置动画。我一开始确实使用CABasicAnimation
过,但是由于这个循环一遍又一遍,我需要最有效的方法来做到这一点。
以下代码动画正确:
private func animateDidBecomeStraight () {
CATransaction.flush()
CATransaction.begin()
CATransaction.setAnimationDuration(0.2)
self.myShape.fillColor = UIColor.Red.cgColor
self.myShape.lineWidth = 1
self.myShape.strokeColor = UIColorRed.cgColor
CATransaction.commit()
}
private func animateDidBecomeUnStraight () {
CATransaction.begin()
CATransaction.setAnimationDuration(0.5)
self.myShape.fillColor = UIColor(white: 1, alpha: 1).cgColor
self.myShape.strokeColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5).cgColor
self.myShape.lineWidth = 0.1
CATransaction.commit()
}
Strait and Unstraight 是按顺序调用的,从不重复调用。
问题
第一次,动画以适当的持续时间工作。当 Strait 第一次打电话和 Unstraight 第一次打电话时。在那之后,他们似乎失去了动画持续时间。变化似乎是瞬间的。