如何在 CALayer 中为图像制作动画

How to animate images in CALayer

在我的 CALayer 中,我正在尝试为图像制作动画,但动画层未显示。我在这里做错了什么:

return CALayer().build {

        let myLayer = CALayer()

        let animation = CAKeyframeAnimation(keyPath: "contents")
        animation.fillMode = kCAFillModeForwards
        animation.duration = 3
        animation.values = TimeLineVC.likeInteractionArray.map {
            [=10=].cgImage as AnyObject
        }
        animation.repeatCount = 3

        myLayer.frame = [=10=].bounds
        myLayer.add(animation, forKey: "contents")
        [=10=].insertSublayer(myLayer, above: [=10=])
        myLayer.backgroundColor = UIColor.black.cgColor


      }

添加一些属性后对我有用:

let animation = CAKeyframeAnimation(keyPath: "contents")
        animation.calculationMode = kCAAnimationDiscrete
        animation.duration = 3.0
        animation.repeatCount = .greatestFiniteMagnitude
        animation.autoreverses = false
        animation.values = TimeLineVC.likeInteractionArray.map {[=10=].cgImage!}
        animation.isRemovedOnCompletion = false
        animation.fillMode = kCAFillModeForwards
        animation.beginTime = 0.0
        [=10=].add(animation, forKey: "contents")