swift 的水波动画或雷达动画效果

刚接触不久iOS开发,拖拉拽开始学习,代码还没接触过,但还算看的半懂!

做成动画扩散效果,这图不是动态的,是我直接截图下来的;

拿别人的代码自己删改了一些,然后加一些自己理解的注释上去,就成这样了!记录下来

import UIKit

class ViewController: UIViewController {
    
    private let radarAnimation = "radarAnimation"   //对象1
    private var animationLayer: CALayer?        //对象2
    private var animationGroup: CAAnimationGroup?       //对象3


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        let first = makeRadarAnimation(showRect: CGRect(x: 120, y: 150, width: 140, height: 140), isRound: true)    //调用 方法:makeRadarAnimation(...)
        view.layer.addSublayer(first)   //显示动画 调用显示
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //动作-开始 按钮
    @IBAction func startAction(_ sender: UIButton) {
        animationLayer?.add(animationGroup!, forKey: radarAnimation)
    }
    //动作-停止 按钮
    @IBAction func stopAction(_ sender: UIButton) {
        animationLayer?.removeAnimation(forKey: radarAnimation)
    }
    
    // 动态波的方法
    private func makeRadarAnimation(showRect: CGRect, isRound: Bool) -> CALayer {
        // 1. 一个动态波
        let shapeLayer = CAShapeLayer()
        shapeLayer.frame = showRect
        // showRect 最大内切圆
        shapeLayer.path = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: showRect.width, height: showRect.height)).cgPath
        shapeLayer.fillColor = UIColor.white.cgColor    //波纹颜色
        shapeLayer.opacity = 0.0    // 默认初始颜色透明度
        animationLayer = shapeLayer     //全局对象2 animationLayer
        
        // 2. 创建动画组 from -> to 透明比例过渡
        let opacityAnimation = CABasicAnimation(keyPath: "opacity")
        opacityAnimation.fromValue = NSNumber(floatLiteral: 1.0)  // 开始透明度
        opacityAnimation.toValue = NSNumber(floatLiteral: 0)      // 结束时透明底
        
        // 3. 波纹动画 起始大小
        let scaleAnimation = CABasicAnimation(keyPath: "transform")
        if isRound {
            scaleAnimation.fromValue = NSValue.init(caTransform3D: CATransform3DScale(CATransform3DIdentity, 1.0, 1.0, 0))      // 缩放起始大小
        } else {
            scaleAnimation.fromValue = NSValue.init(caTransform3D: CATransform3DScale(CATransform3DIdentity, 1.5, 1.5, 0))      // 缩放起始大小
        }
        scaleAnimation.toValue = NSValue.init(caTransform3D: CATransform3DScale(CATransform3DIdentity, 2.0, 2.0, 0))      // 缩放结束大小
        
        // 4. 定义波的运行时间
        let animationGroup = CAAnimationGroup()
        animationGroup.animations = [opacityAnimation, scaleAnimation]  //引用opacityAnimation 和 scaleAnimation
        animationGroup.duration = 3.0       // 动画执行时间
        animationGroup.repeatCount = HUGE   // 最大重复
        animationGroup.autoreverses = false
        
        self.animationGroup = animationGroup    //全局对象3  animationGroup
        shapeLayer.add(animationGroup, forKey: radarAnimation)  //全局对象1 radarAnimation
        
        // 5. 需要重复的动态波,数量,缩放起始点 <=> 创建副本
        let replicator = CAReplicatorLayer()
        replicator.frame = shapeLayer.bounds
        replicator.instanceCount = 4
        replicator.instanceDelay = 1.0
        replicator.addSublayer(shapeLayer)
        
        return replicator
    }

}

参考来源:https://blog.csdn.net/xxh0307/article/details/80069007

猜你喜欢

转载自blog.csdn.net/qq_41408081/article/details/85772470
今日推荐