iOS-CAReplicatorLayer实现音量跳动动画

目标

我们要实现一个音量跳动动画,之前用CAShapeLayer实现过,这次我们用CAReplicatorLayer来实现不同的风格

实现过程

self.view.backgroundColor = .black
        
        let replicatorLayer = CAReplicatorLayer()
        replicatorLayer.frame = CGRect(x: 0, y: 0, width: 414, height: 200)
        replicatorLayer.instanceCount = 20;
        replicatorLayer.instanceTransform = CATransform3DMakeTranslation(20, 0, 0)
        replicatorLayer.instanceDelay = 0.2
        replicatorLayer.masksToBounds = true
        replicatorLayer.backgroundColor = UIColor.black.cgColor;
        
        let layer = CALayer()
        layer.frame = CGRect(x: 14, y: 200, width: 10, height: 100)
        layer.backgroundColor = UIColor.red.cgColor

        replicatorLayer.addSublayer(layer)
        self.view.layer.addSublayer(replicatorLayer)
        
        let animation = CABasicAnimation()
        animation.keyPath = "position.y"
        animation.duration = 0.5
        animation.fromValue = 200
        animation.toValue = 180
        animation.autoreverses = true
        animation.repeatCount = MAXFLOAT
        layer.add(animation, forKey: nil)

效果图

iOS-CAReplicatorLayer实现音量跳动动画_第1张图片
音量跳动效果图.gif

如果觉得我的文章对您有用,请点击喜欢。您的支持将鼓励我继续创作!大家有什么不懂或我哪里写错了都可以评论留言,我一定会回复的~

你可能感兴趣的:(iOS-CAReplicatorLayer实现音量跳动动画)