animation

//
//  HomeViewController.swift
//  Viking
//
//  Created by 雪衣侯 on 2020/7/24.
//  Copyright © 2020 雪衣侯. All rights reserved.
//
import UIKit
class HomeViewController: BaseViewController {
    
    lazy var vm: HomeViewModel = HomeViewModel(self)
    lazy var v: HomeView = HomeView(frame: CGRect.zero, vm: self.vm)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .systemBlue
        shapeLayer.add(animationGroup, forKey: "animationGroup")
        heartImgView.layer.add(setUpAnimation(), forKey: "scale")
        rotaImgView.layer.add(rotationAnimation(), forKey: "rotationAnimation")
    }
    
    override func ai_setupViews() {
        view.addSubview(v)
        view.addSubview(bgView)
        bgView.layer.addSublayer(replicatorLayer)
        replicatorLayer.addSublayer(shapeLayer)
        replicatorLayer.addSublayer(rotaImgView.layer)
        replicatorLayer.addSublayer(heartImgView.layer)
        view.addSubview(rotaImgView)
        view.addSubview(heartImgView)
    }
    
    override func ai_setupLayout() {
        v.snp.makeConstraints{(make) in
            make.edges.equalToSuperview()
        }
        
        bgView.snp.makeConstraints{(make) in
            make.edges.equalToSuperview()
        }
        
        rotaImgView.snp.makeConstraints{(make) in
            make.width.height.equalTo(120)
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        heartImgView.snp.makeConstraints{(make) in
            make.width.height.equalTo(60)
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
       
    }
    
    lazy var bgView: UIImageView = {
        let new = UIImageView()
        new.image = UIImage(named: "heart_bg")
        return new
    }()
    
    lazy var heartImgView: UIImageView = {
        let new = UIImageView()
        new.image = UIImage(named: "heart")
        return new
    }()
    
    lazy var rotaImgView: UIImageView = {
        let new = UIImageView()
        new.image = UIImage(named: "online_black_circle")
        return new
    }()
    
    let shapeLayer: CAShapeLayer = {
        let shapeLayer = CAShapeLayer()
        shapeLayer.frame = CGRect(x: (UIScreen.main.bounds.width-400)/2, y: (UIScreen.main.bounds.height-400)/2, width: 400, height: 400)
        shapeLayer.path = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 400, height: 400)).cgPath
        shapeLayer.fillColor = UIColor(red: 198 / 255.0, green: 201 / 255.0, blue: 220 / 255.0, alpha: 1.0).cgColor //蓝色
        shapeLayer.opacity = 0.0
        return shapeLayer
    }()
    
    lazy var replicatorLayer: CAReplicatorLayer = {
        let replicatorLayer = CAReplicatorLayer()
        replicatorLayer.frame = CGRect(x: 0, y: 0, width: 400, height: 400)
        replicatorLayer.instanceDelay = 0.4
        replicatorLayer.instanceCount = 3
        replicatorLayer.addSublayer(shapeLayer)
        return replicatorLayer
    }()
    
    lazy var animationGroup: CAAnimationGroup = {
        let animationGroup = CAAnimationGroup()
        animationGroup.animations = [alphaAnimation(), scaleAnimation()]
        animationGroup.duration = 1.5
        animationGroup.autoreverses = false
        animationGroup.repeatCount = MAXFLOAT
        animationGroup.isRemovedOnCompletion = false
        return animationGroup
    }()
    
    //    func alphaAnimation() -> CABasicAnimation {
    //        let alpha = CABasicAnimation(keyPath: "opacity")
    //        alpha.fromValue = NSNumber(value: 1.0)
    //        alpha.toValue = NSNumber(value: 0.0)
    //        return alpha
    //    }
    
    //    func scaleAnimation() -> CABasicAnimation{
    //        let scale = CABasicAnimation(keyPath: "transform")
    //        scale.fromValue = NSValue(caTransform3D: CATransform3DScale(CATransform3DIdentity, 0.0, 0.0, 0.0))
    //        scale.toValue = NSValue(caTransform3D: CATransform3DScale(CATransform3DIdentity, 1.5, 1.5, 0.0))
    //        return scale
    //    }
    
    //波浪放大动画
    func scaleAnimation() -> CABasicAnimation {
        let animation = CABasicAnimation(keyPath: "transform.scale.xy")
        animation.fromValue = NSNumber(value: 0)
        animation.toValue = NSNumber(value: 1)
        return animation
    }
    
    //波浪透明度动画
    func alphaAnimation() -> CAKeyframeAnimation {
        let animation = CAKeyframeAnimation(keyPath: "opacity")
        animation.values = [0.4,0.6,0]
        animation.keyTimes = [0,0.4,1]    //第一个时间值必须为0,列表中的最后一个时间值必须为1
        return animation
    }
    
    //心跳动画
    func setUpAnimation() -> CABasicAnimation{
        let scalAnima = CABasicAnimation(keyPath: "transform.scale.xy")
        scalAnima.fromValue = NSNumber(value:1)
        scalAnima.toValue = NSNumber(value:1.6)
        scalAnima.duration = 0.2
        scalAnima.autoreverses = true
        scalAnima.repeatCount = MAXFLOAT
        scalAnima.isRemovedOnCompletion = false
        return scalAnima
    }
    
    //扇形旋转动画
    func rotationAnimation() -> CABasicAnimation{
        let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotationAnimation.toValue = Double.pi * 2
        rotationAnimation.duration = 0.6
        rotationAnimation.isCumulative = true
        rotationAnimation.repeatCount = MAXFLOAT
        rotationAnimation.isRemovedOnCompletion = false
        return rotationAnimation
    }
    
}



你可能感兴趣的:(animation)