【发布时间】:2023-07-16 21:08:01
【问题描述】:
我有一个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 第一次打电话时。在那之后,他们似乎失去了动画持续时间。变化似乎是立竿见影的。
【问题讨论】:
-
那么目标是什么?它是笔直的,直的,直的,直的……永远的吗?如果是这样,你的“循环”在哪里?我看不出你是如何触发这些动画的,当然也不是一个接一个。此外,CABasicAnimation 实际上是执行此操作的正确方法,因此请显示那个代码。
标签: swift xcode cashapelayer catransaction