Ios 如何正确设置CABASICIATION(开始)时间?

Ios 如何正确设置CABASICIATION(开始)时间?,ios,delay,cabasicanimation,Ios,Delay,Cabasicanimation,我使用CABasicAnimation为存储在数组中的CAShapeLayers设置颜色动画。动画显示不稳定,具体取决于动画。持续时间,我不知道为什么。我怀疑animation.beginTime=CACurrentMediaTime()+延迟有问题 动画描述 动画包括连续将形状闪烁为黄色,然后在动画结束后将其变为黑色 动画的当前状态 当动画持续时间超过某个时间时,它将正常工作 例如,持续时间为2秒: 但是,当我缩短持续时间时,结果就大不相同了 例如,持续时间为1秒: 您将注意到,动

我使用CABasicAnimation为存储在数组中的
CAShapeLayers
设置颜色动画。动画显示不稳定,具体取决于
动画。持续时间
,我不知道为什么。我怀疑animation.beginTime=CACurrentMediaTime()+延迟有问题

动画描述

动画包括连续将形状闪烁为黄色,然后在动画结束后将其变为黑色

动画的当前状态

当动画持续时间超过某个时间时,它将正常工作

  • 例如,持续时间为2秒:

但是,当我缩短持续时间时,结果就大不相同了

  • 例如,持续时间为1秒:

您将注意到,动画已经缓存/结束了前10个左右的条,然后等待并开始为其余形状设置动画

  • 同样,在0.5s的持续时间内:

在这种情况下,在一段时间后显示一些动画之前,似乎有更多的动画已经结束(形状为黑色)。您还可以注意到,虽然形状颜色动画应该持续相同的持续时间(0.5s),但有些动画感觉比其他动画快

代码

在UIViewController类的ViewDidAspect方法中调用动画

我已经创建了一个UIView自定义类来绘制我的形状,并使用该类的扩展来设置它们的动画

用于设置颜色动画的代码:

    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中运行),如怀疑的那样,形状没有动画

我怀疑beginTime的某些功能无法正常工作,但可能不是这样,或者只是因为即使在形状设置动画时,形状动画的持续时间似乎也会发生变化,而不应该发生变化

非常感谢您提前了解这个问题。任何想法都是受欢迎的,即使它看起来很牵强,它可以打开新的方式来解决这个问题


最好的是,

事实上,持续时间和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) {... }