//
//  PQActivityIndicatorView.swift
//  PQSpeed
//
//  Created by ak on 2020/9/23.
//  Copyright © 2020 BytesFlow. All rights reserved.
//  功能： 显示 UIActivityIndicatorView

import UIKit
import BFCommonKit
 
class PQActivityIndicatorView: UIView {
    func showHud(isCovered: Bool = false) {
        hideHud()
        let frame = CGRect(x: 0, y: 0, width: 78, height: 78)

        let backVFrame = isCovered == false ? frame : CGRect(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth)
        self.frame = backVFrame
        let backV = UIView(frame: backVFrame)
        backV.center = center
        backV.tag = 8421
        addSubview(backV)
        backgroundColor = UIColor.clear

        let hudV = UIView(frame: frame)
        hudV.center = CGPoint(x: backV.frame.width / 2, y: backV.frame.height / 2)
        hudV.layer.cornerRadius = 12
        hudV.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.8)
//        hudV.backgroundColor = .green
        backV.addSubview(hudV)

        let indicatorView = UIActivityIndicatorView(style: .whiteLarge)
        indicatorView.frame = CGRect(x: 21, y: 21, width: 36, height: 36)
        indicatorView.startAnimating()
        hudV.addSubview(indicatorView)

        hudV.alpha = 0.0
        UIView.animate(withDuration: 0.2, animations: {
            hudV.alpha = 1
        })
    }

    func hideHud() {
        let backV = viewWithTag(8421)
        guard let backv = backV else { return }
        backv.removeFromSuperview()
        removeFromSuperview()
    }
}
