如何在CALayer中为图像添加动画效果

时间:2017-07-04 06:48:45

标签: ios swift calayer

在我的CALayer我试图动画图像,但动画图层没有显示。我在这里做错了什么:

return CALayer().build {

        let myLayer = CALayer()

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

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


      }

1 个答案:

答案 0 :(得分:0)

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

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