简体   繁体   中英

How to animate images in CALayer

In my CALayer i am trying to animate images but the animation layer is not showing up. What i am doing wrong in here:

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


      }

It worked for me after adding a few properties:

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM