CAKeyframeAnimation не соответствует времени видео продолжительности - PullRequest
0 голосов
/ 16 апреля 2020

Привет, я хотел бы наложить текущую скорость на видео с наложением (AVVideoCompositionCoreAnimationTool)

создал текстовый слой и добавить к нему атрибутивный текст.

пытался использовать CAKeyframeAnimation для изменения текст каждую секунду до текущей скорости.

анимация не работает правильно. если продолжительность видеоролика составляет 10 секунд, он считается только до 3., но количество шагов составляет 1 секунду, поэтому оно должно считаться до 10, каждая секунда - 1 шаг.

Можете ли вы мне помочь?

        let attributedText = NSAttributedString( string: text, attributes: [
            .font: UIFont(name: "ArialRoundedMTBold", size: 60) as Any,
            .foregroundColor: UIColor.purple,
            .strokeColor: UIColor.white,
            .strokeWidth: -3])

        let textLayer = CATextLayer()
        textLayer.string = attributedText
        textLayer.shouldRasterize = true
        textLayer.rasterizationScale = UIScreen.main.scale
        textLayer.backgroundColor = UIColor.clear.cgColor
        textLayer.alignmentMode = .center
        textLayer.frame = CGRect( x: 0, y: videoSize.height * 0.66, width: videoSize.width, height: 150)
        textLayer.displayIfNeeded()

        let stepDuration = 1.0
        var values: [NSAttributedString] = []
        var keytimes:[NSNumber] = []

        stride(from: 0.0, to: duration , by: stepDuration).forEach { i in
            values.append(NSAttributedString( string: "Sky.\(i)", attributes: [
                            .font: UIFont(name: "ArialRoundedMTBold", size: 60) as Any,
                            .foregroundColor: UIColor.red,
                            .strokeColor: UIColor.white,
                            .strokeWidth: -3]))
        }

        for i in 0...values.count - 1 {
            keytimes.append(NSNumber(value: Double(i) * stepDuration / duration))
        }

            let animation = CAKeyframeAnimation(keyPath: "string")
            animation.calculationMode = .linear
            animation.duration = duration // is total video duration
            animation.values = values
            animation.keyTimes = keytimes
            animation.isRemovedOnCompletion = true
            animation.fillMode = .forwards
            animation.beginTime = CACurrentMediaTime() //AVCoreAnimationBeginTimeAtZero
        textLayer.add(animation, forKey: nil)
...