Ios 如何正确设置CABASICIATION(开始)时间?
我使用CABasicAnimation为存储在数组中的Ios 如何正确设置CABASICIATION(开始)时间?,ios,delay,cabasicanimation,Ios,Delay,Cabasicanimation,我使用CABasicAnimation为存储在数组中的CAShapeLayers设置颜色动画。动画显示不稳定,具体取决于动画。持续时间,我不知道为什么。我怀疑animation.beginTime=CACurrentMediaTime()+延迟有问题 动画描述 动画包括连续将形状闪烁为黄色,然后在动画结束后将其变为黑色 动画的当前状态 当动画持续时间超过某个时间时,它将正常工作 例如,持续时间为2秒: 但是,当我缩短持续时间时,结果就大不相同了 例如,持续时间为1秒: 您将注意到,动
CAShapeLayers
设置颜色动画。动画显示不稳定,具体取决于动画。持续时间
,我不知道为什么。我怀疑animation.beginTime=CACurrentMediaTime()+延迟有问题
动画描述
动画包括连续将形状闪烁为黄色,然后在动画结束后将其变为黑色
动画的当前状态
当动画持续时间超过某个时间时,它将正常工作
- 例如,持续时间为2秒:
- 例如,持续时间为1秒:
- 同样,在0.5s的持续时间内:
enum ColorAnimation{
case continuousSwap
case continousWithNewColor(color: UIColor)
case randomSwap
case randomWithNewColor(color: UIColor)
case randomFromUsedColors
}
func animateColors(for duration: Double,_ animationType: ColorAnimation, colorChangeDuration swapColorDuration: Double){
guard abs(swapColorDuration) != Double.infinity else {
print("Error in defining the shape color change duration")
return
}
let animDuration = abs(duration)
let swapDuration = abs(swapColorDuration)
let numberOfSwaps = Int(animDuration / min(swapDuration, animDuration))
switch animationType {
case .continousWithNewColor(color: let value):
var fullAnimation = [CABasicAnimation]()
for i in (0...numberOfSwaps) {
let index = i % (self.pLayers.count)
let fromValue = pLayers[index].pattern.color
let delay = Double(i) * swapDuration / 3
let anim = colorAnimation(for: swapDuration, fromColor: value, toColor: fromValue, startAfter: delay)
fullAnimation.append(anim)
}
for i in (0...numberOfSwaps) {
CATransaction.begin()
let index = i % (self.pLayers.count)
CATransaction.setCompletionBlock {
self.pLayers[index].shapeLayer.fillColor = UIColor.black.cgColor
}
pLayers[index].shapeLayer.add(fullAnimation[i], forKey: "fillColorShape")
CATransaction.commit()
}
default:
()
}
}
通过颜色变化的持续时间分段动画的整个持续时间(例如,如果整个动画为10s,每个形状在1s内改变颜色,则表示10个形状将改变颜色)。
然后,我使用colorAnimation方法(for:fromColor、toColor、startAfter:)创建CABasicaAnimation对象
最后,我将动画添加到图层中。
代码显然可以优化,但我选择继续执行这些步骤,试图找出它无法正常工作的原因
迄今为止的尝试次数
到目前为止,我已经尝试:
- 在colorAnimation方法中设置和不设置
,包括使用和不使用animation.beginTime
:如果不使用CACurrentMediaTime()
设置CACurrentMediaTime
,我根本看不到任何动画animation.beginTime
- 有无指向
:它没有改变任何东西animation.delegate=self
- 使用DispatchQueue(将动画存储在全局中,并在main中运行),如怀疑的那样,形状没有动画
最好的是,事实上,持续时间和swapColorDuration之间存在关系
func animateColors(持续时间:双精度,\uanimationType:ColorAnimation,colorChangeDuration SwapColor持续时间:双精度)
当你打电话时,你可能需要保持这种关系
let colorChangeDuration: TimeInterval = 0.5
animateColors(for: colorChangeDuration * TimeInterval(pLayers.count), .continousWithNewColor(color: UIColor.black), colorChangeDuration: colorChangeDuration)
这里也有:
let numberOfSwaps = Int(animDuration / min(swapDuration, animDuration)) - 1
该值可能比您需要的稍高
或
问题在于这个let index=i%(self.pLayers.count)
如果numberOfSwaps>self.pLayers.count
,则某些乐队将是双重动画
let numberOfSwaps1 = Int(animDuration / min(swapDuration, animDuration))
let numberOfSwaps = min(numberOfSwaps1, self.pLayers.count)
其余的是
for i in (0..<numberOfSwaps) {... }
中的i(0..感谢您观看!事实上,duration
是完整动画的时间,swapDuration
是实际颜色变化的持续时间。问题中解释了它们之间的关系:运行颜色变化动画的x倍,x是持续时间/swapDuration产生的整数。在所有示例中我将持续时间设置为40秒,而swapDuration
分别设置为2秒、1秒、0.5秒。因此,我不明白为什么这种关系可以解释动画行为。最后一行将解决最后一分钟的波段问题。可能值大于必要值。这里的关系是如果swapDuration是小的,numberOfSwaps将大于shapeLayer的计数。因此,在动画实际发生之前,还有更多的时间等待。我对你的想法进行了一些讨论,它确实产生了影响。我确实意识到,如果持续时间/交换持续时间的比率低于玩家。count
,动画显示p正确。我正在挖掘…我会回来报告的!谢谢!我知道你做了什么,谢谢!但问题是:如果动画的持续时间大于所有动画的总和,我希望一些乐队将动画增加一倍…如果是两个动画,它们的关键点应该不同。pLayers[index].shapeLayer.add(fullAnimation[I],forKey:“fillColorShape”)
for i in (0..<numberOfSwaps) {... }