MG--Swift遮照HUD 2

MG--Swift遮照HUD 2

比如網(wǎng)絡請求IO讀寫操作這個操作往往比較耗時這個時候我們往往不需要用戶操作這個時候我們就應該提供一個蒙版遮照來防止用戶操作就需要用到下面的方法了下面是寫的蒙版提示界面我們一般每寫的一個界面度會涉及到網(wǎng)絡APi請求所以需要HUD


2種遮照效果

image

image

代碼實現(xiàn)如下

//
//  MGSquaresAnimationView.swift
//  MGSDKDemo
//  Created by LYM-mg on 2021/1/27.
//  Copyright ? 2021 LYM-mg. All rights reserved.
//

import UIKit

public enum MGSquaresAnimationViewType {
    case Square(animationLayerTintColor: UIColor, squareSize: CGFloat)
    case Circle(ballWidth: CGFloat)
}

public class MGSquaresAnimationView: UIView {
    convenience init(frame: CGRect = UIScreen.main.bounds, animationViewType: MGSquaresAnimationViewType = .Circle(ballWidth: 13)) {
        self.init(frame: frame)
        switch animationViewType {
            case .Circle(let ballWidth):
                self._ballWidth = ballWidth
                self.initBallView()
                break
            case .Square(let animationLayerTintColor, let squareSize):
                self.initSquareView()
                self.animationLayer.frame = self.bounds
                self.animationLayerTintColor = animationLayerTintColor
                self.squareSize = squareSize
                
                self.setupAnimation()
                
                let tintColorRef = animationLayerTintColor.cgColor
                animationLayer.backgroundColor = tintColorRef
                break
        }
        self.animationViewType = animationViewType
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        print("\(self)--deinit")
    }
    
    // MARK: - Square
    fileprivate var animationLayer: CALayer = CALayer()
    fileprivate var isAnimating: Bool = false
    fileprivate var animationLayerTintColor: UIColor = .white {
        didSet {
            let tintColorRef = animationLayerTintColor.cgColor
            for subLayer in animationLayer.sublayers ?? [] {
                subLayer.backgroundColor = tintColorRef
                if subLayer is CAShapeLayer {

                    let shapeLayer = CAShapeLayer()
                    shapeLayer.strokeColor = tintColorRef
                    shapeLayer.fillColor = tintColorRef
                }
            }
        }
    }
    
    fileprivate var squareSize: CGFloat = 60 {
        didSet {
            self.setupAnimation()
        }
    }
    
    fileprivate var animationViewType: MGSquaresAnimationViewType = .Circle(ballWidth: 13)
    
    func initSquareView() {
//        isUserInteractionEnabled = false
        isHidden = false
        layer.addSublayer(animationLayer)
    }
    
    func startAnimating() {
        switch animationViewType {
            case .Circle(_):
                startPathAnimation()
                break
            case .Square(_ , _):
                if (animationLayer.sublayers != nil) {
                    setupAnimation()
                }
                isHidden = false
                animationLayer.speed = 1.0
                break
        }
       isAnimating = true
    }

    func stopAnimating() {
        switch animationViewType {
            case .Circle(_):
                 ball1.layer.removeAllAnimations()
                 ball1.removeFromSuperview()
                 ball2.layer.removeAllAnimations()
                 ball2.removeFromSuperview()
                 ball3.layer.removeAllAnimations()
                 ball3.removeFromSuperview()
            case .Square(animationLayerTintColor , squareSize):
                animationLayer.speed = 0.0
                isHidden = true
            default:
                break
        }
        isAnimating = false
    }
    
    func setupAnimation() {

        animationLayer.sublayers = nil

        setupAnimation(layer: animationLayer, with: CGSize(width: self.squareSize, height: self.squareSize), tintColor: self.animationLayerTintColor)
        animationLayer.speed = 0.0
    }
    
    func setupAnimation(layer: CALayer, with size: CGSize, tintColor: UIColor) {
        let beginTime = TimeInterval(CACurrentMediaTime())

        let squareSize = floor(size.width / 4.0)

        let oX: CGFloat = (layer.bounds.size.width - size.width) / 2.0
        let oY: CGFloat = (layer.bounds.size.height - size.height) / 2.0
        let colors = [
            UIColor(red: 0.880, green: 0.409, blue: 0.195, alpha: 1),
            UIColor(red: 0.137, green: 0.412, blue: 0.663, alpha: 1),
            UIColor(red: 0.810, green: 0.709, blue: 0.115, alpha: 1),
            UIColor(red: 0.537, green: 0.492, blue: 0.363, alpha: 1)
        ]
        
        for i in 0..<5 {
            let square = CALayer()
            square.frame = CGRect(x: oX, y: oY, width: squareSize, height: squareSize)
            square.anchorPoint = CGPoint(x: 0.5, y: 0.5)
            square.backgroundColor = colors[i].cgColor
            square.shouldRasterize = true
            square.rasterizationScale = UIScreen.main.scale

            let transformAnimation = createKeyframeAnimation(withKeyPath: "transform")
            transformAnimation.duration = 1.6
            transformAnimation.beginTime = beginTime - (Double(i) * (transformAnimation.duration) / 4.0)
            transformAnimation.repeatCount = MAXFLOAT

            transformAnimation.keyTimes = [NSNumber(value: 0.0), NSNumber(value: 0.25), NSNumber(value: 0.50), NSNumber(value: 0.75), NSNumber(value: 1.0)]
            
            transformAnimation.timingFunctions = [
                CAMediaTimingFunction(name: .easeInEaseOut),
                CAMediaTimingFunction(name: .easeInEaseOut),
                CAMediaTimingFunction(name: .easeInEaseOut),
                CAMediaTimingFunction(name: .easeInEaseOut),
                CAMediaTimingFunction(name: .easeInEaseOut)
            ]
            
            var t1 = CATransform3DMakeTranslation(size.width - squareSize, 0.0, 0.0)
            t1 = CATransform3DRotate(t1, degreesToRadians(-90.0), 0.0, 0.0, 1.0)
            t1 = CATransform3DScale(t1, 0.7, 0.7, 1.0)

            var t2 = CATransform3DMakeTranslation(size.width - squareSize, size.height - squareSize, 0.0)
            t2 = CATransform3DRotate(t2, degreesToRadians(-180.0), 0.0, 0.0, 1.0)
            t2 = CATransform3DScale(t2, 1.0, 1.0, 1.0)

            var t3 = CATransform3DMakeTranslation(0.0, size.height - squareSize, 0.0)
            t3 = CATransform3DRotate(t3, degreesToRadians(-270.0), 0.0, 0.0, 1.0)
            t3 = CATransform3DScale(t3, 0.7, 0.7, 1.0)

            var t4 = CATransform3DMakeTranslation(0.0, 0.0, 0.0)
            t4 = CATransform3DRotate(t4, degreesToRadians(-360.0), 0.0, 0.0, 1.0)
            t4 = CATransform3DScale(t4, 1.0, 1.0, 1.0)
            
            transformAnimation.values = [
                NSValue(caTransform3D: CATransform3DIdentity),
                NSValue(caTransform3D: t1),
                NSValue(caTransform3D: t2),
                NSValue(caTransform3D: t3),
                NSValue(caTransform3D: t4)
            ]

            layer.addSublayer(square)
            square.add(transformAnimation, forKey: "animation")
        }
    }
    
    private func degreesToRadians(_ degrees: Double) -> CGFloat {
        return CGFloat((degrees) / 180.0 * Double.pi)
    }
    
    private func createAnimationGroup() -> CAAnimationGroup {
        let animationGroup = CAAnimationGroup()
        animationGroup.isRemovedOnCompletion = false
        return animationGroup
    }
    
    private func createBasicAnimation(withKeyPath keyPath: String?) -> CABasicAnimation {
        let animation = CABasicAnimation(keyPath: keyPath)
        animation.isRemovedOnCompletion = false
        return animation
    }

    private func createKeyframeAnimation(withKeyPath keyPath: String?) -> CAKeyframeAnimation {
        let animation = CAKeyframeAnimation(keyPath: keyPath)
        animation.isRemovedOnCompletion = false
        return animation
    }
    
    
    // MARK: - Circle
    var ball1: UIView = UIView()
    var ball2: UIView = UIView()
    var ball3: UIView = UIView()
    var _ballWidth: CGFloat = 13
    var ballContainer: UIVisualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
    
    func initBallView() {
        ballContainer.frame = CGRect(x: 0, y: 0, width: 120, height: 120)
        ballContainer.center = CGPoint(x: bounds.size.width / 2.0, y: bounds.size.height / 2.0)
        ballContainer.layer.cornerRadius = 10.0
        ballContainer.layer.masksToBounds = true
        ballContainer.backgroundColor = UIColor.clear
        addSubview(ballContainer)

        let margin: CGFloat = 3.0

        ball1 = UIView(frame: CGRect(x: 0, y: 0, width: _ballWidth, height: _ballWidth))
        ball1.center = CGPoint(x: _ballWidth / 2.0 + margin, y: ballContainer.bounds.size.height / 2.0)
        ball1.layer.cornerRadius = _ballWidth / 2.0
        ball1.backgroundColor = UIColor(red: 54 / 255.0, green: 136 / 255.0, blue: 250 / 255.0, alpha: 1)
        ballContainer.contentView.addSubview(ball1)
        
        ball2 = UIView(frame: CGRect(x: 0, y: 0, width: _ballWidth, height: _ballWidth))
        ball2.center = CGPoint(x: ballContainer.bounds.size.width / 2.0, y: ballContainer.bounds.size.height / 2.0)
        ball2.layer.cornerRadius = _ballWidth / 2.0
        ball2.backgroundColor = UIColor(red: 100 / 255.0, green: 100 / 255.0, blue: 100 / 255.0, alpha: 1)
        ballContainer.contentView.addSubview(ball2)

        ball3 = UIView(frame: CGRect(x: 0, y: 0, width: _ballWidth, height: _ballWidth))
        ball3.center = CGPoint(x: ballContainer.bounds.size.width - _ballWidth / 2.0 - margin, y: ballContainer.bounds.size.height / 2.0)
        ball3.layer.cornerRadius = _ballWidth / 2.0
        ball3.backgroundColor = UIColor(red: 234 / 255.0, green: 67 / 255.0, blue: 69 / 255.0, alpha: 1)
        ballContainer.contentView.addSubview(ball3)
    }
    
    func startPathAnimation() {
        let ballScale: CGFloat = 1.3
        //-------第一個球的動畫
        let width = ballContainer.bounds.size.width
        //小圓半徑
        let r: CGFloat = (ball1.bounds.size.width) * ballScale / 2.0
        //大圓半徑
        let R = (width / 2 + r) / 2.0

        let path1 = UIBezierPath()
        path1.move(to: ball1.center)
        //畫大圓
        path1.addArc(withCenter: CGPoint(x: R + r, y: width / 2), radius: R, startAngle: .pi, endAngle: .pi * 2, clockwise: false)
        //畫小圓
        let path1_1 = UIBezierPath()
        path1_1.addArc(withCenter: CGPoint(x: width / 2, y: width / 2), radius: r * 2, startAngle: .pi * 2, endAngle: .pi, clockwise: false)
        path1.append(path1_1)
        //回到原處
        path1.addLine(to: ball1.center)
        //執(zhí)行動畫
        let positionAnimation = CAKeyframeAnimation(keyPath: "position")
        positionAnimation.path = path1.cgPath
        positionAnimation.isRemovedOnCompletion = false
        positionAnimation.repeatCount = MAXFLOAT
        positionAnimation.duration = 1.6
        positionAnimation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
        ball1.layer.add(positionAnimation, forKey: "animation1")
        
        
        //-------第三個球的動畫
        let path3 = UIBezierPath()
        path3.move(to: ball3.center)
        //畫大圓
        path3.addArc(withCenter: CGPoint(x: width - (R + r), y: width / 2), radius: R, startAngle: 2 * .pi, endAngle: .pi, clockwise: false)
        //畫小圓
        let path3_1 = UIBezierPath()
        path3_1.addArc(withCenter: CGPoint(x: width / 2, y: width / 2), radius: r * 2, startAngle: .pi, endAngle: .pi * 2, clockwise: false)
        path3.append(path3_1)
        //回到原處
        path3.addLine(to: ball3.center)
        //執(zhí)行動畫
        let animation3 = CAKeyframeAnimation(keyPath: "position")
        animation3.path = path3.cgPath
        animation3.repeatCount = MAXFLOAT
        animation3.isRemovedOnCompletion = false
        animation3.duration = 1.6
        animation3.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
        ball3.layer.add(animation3, forKey: "animation3")
        
        // 放大縮小動畫
        let scaleAnimation = createBasicAnimation(withKeyPath: "transform.scale")
        scaleAnimation.fromValue = 1.0
        scaleAnimation.toValue = 1.3
        scaleAnimation.duration = 1.6/2
        scaleAnimation.isRemovedOnCompletion = false
        scaleAnimation.repeatCount = MAXFLOAT
        scaleAnimation.autoreverses = true
        scaleAnimation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
        ball1.layer.add(scaleAnimation, forKey: "scale")
        ball2.layer.add(scaleAnimation, forKey: "scale")
        ball3.layer.add(scaleAnimation, forKey: "scale")
    }
}

調用

let view = MGSquaresAnimationView(animationViewType: .Square(animationLayerTintColor: UIColor.red, squareSize: 60))
//        let view = MGSquaresAnimationView(animationViewType: .Circle(ballWidth: 30))
self.view.addSubview(view)
view.startAnimating()        
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
     view.stopAnimating()
     view.removeFromSuperview()
}
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異畜号,居然都是意外死亡,警方通過查閱死者的電腦和手機缠黍,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進店門弄兜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人瓷式,你說我怎么就攤上這事替饿。” “怎么了贸典?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵视卢,是天一觀的道長。 經常有香客問我廊驼,道長据过,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任妒挎,我火速辦了婚禮绳锅,結果婚禮上,老公的妹妹穿的比我還像新娘酝掩。我一直安慰自己鳞芙,他們只是感情好,可當我...
    茶點故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布期虾。 她就那樣靜靜地躺著原朝,像睡著了一般。 火紅的嫁衣襯著肌膚如雪镶苞。 梳的紋絲不亂的頭發(fā)上喳坠,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天,我揣著相機與錄音茂蚓,去河邊找鬼壕鹉。 笑死剃幌,一個胖子當著我的面吹牛,可吹牛的內容都是我干的御板。 我是一名探鬼主播锥忿,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼怠肋!你這毒婦竟也來了?” 一聲冷哼從身側響起淹朋,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤笙各,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后础芍,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體杈抢,經...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年仑性,在試婚紗的時候發(fā)現(xiàn)自己被綠了惶楼。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡诊杆,死狀恐怖歼捐,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情晨汹,我是刑警寧澤豹储,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站淘这,受9級特大地震影響剥扣,放射性物質發(fā)生泄漏。R本人自食惡果不足惜铝穷,卻給世界環(huán)境...
    茶點故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一钠怯、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧曙聂,春花似錦晦炊、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至朦佩,卻和暖如春并思,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背语稠。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工宋彼, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留弄砍,地道東北人。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓输涕,卻偏偏與公主長得像音婶,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子莱坎,可洞房花燭夜當晚...
    茶點故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內容