2018-08-06 swift 自定义hud

项目中用到的图片也可以使用代码画出来。

使用方法

show: YTCustomeHudView.showHudWithText(ShowHudType.loading,"loading...")

dismiss:  YTCustomeHudView.dismissHud()

importFoundation

class YTCustomeHudView :NSObject{

    privatestaticfunccreateLayer() ->CAShapeLayer{

        letpath :UIBezierPath=UIBezierPath(arcCenter:CGPoint(x: 25, y: 25), radius: 17, startAngle: .pi*0.5, endAngle:.pi*0.25, clockwise:false)

        let layer : CAShapeLayer  =CAShapeLayer()

        layer.path= path.cgPath

        layer.lineWidth= 2.5

        layer.fillColor = UIColor.clear.cgColor

        layer.strokeColor = Construction.instance.hex(0x26ABFB).cgColor

        returnlayer

    }

    privatestaticfunccreateAnimation() ->CABasicAnimation{

        letanimation =CABasicAnimation(keyPath:"transform.rotation.z")

        animation.duration= 1.5

        animation.repeatCount=HUGE

        animation.autoreverses=false

        animation.toValue=Double.pi*2

        returnanimation

    }

    class func showHudWithText(type:ShowHudType,msg:String){

        letbgView =UIView()

        bgView.backgroundColor = UIColor.clear

        bgView.tag= 999

        bgView.frame = UIScreen.main.bounds

        UIApplication.shared.keyWindow?.addSubview(bgView)


        lethudView =UIView()

        hudView.backgroundColor = UIColor.black.withAlphaComponent(0.4)

        hudView.frame=CGRect(x: 0, y: 0, width: 120, height: 140)

        hudView.center = CGPoint(x: YTConstants.ScreenWidth/2, y: YTConstants.ScreenHeight/2)

        hudView.layer.cornerRadius= 2

        hudView.layer.masksToBounds=true

        bgView.addSubview(hudView)


        leticonImgView :UIImageView=  UIImageView(frame:CGRect(x: 35, y: 30, width: 50, height: 50))

        iconImgView.image=  ShowHudType.getHudView(type)()

        hudView.addSubview(iconImgView)


        lettitleLabel =UILabel(frame:CGRect(x: 10, y: iconImgView.frame.origin.y+iconImgView.frame.size.height+16, width: 100, height: 24))

        titleLabel.font=UIFont.systemFont(ofSize: 16)

        titleLabel.textColor=UIColor.white

        titleLabel.textAlignment= .center

        titleLabel.numberOfLines= 0

        titleLabel.text= msg

        hudView.addSubview(titleLabel)


        iftype ==ShowHudType.loading{

            iconImgView.layer.insertSublayer(createLayer(), at: 0)

            iconImgView.layer.add(createAnimation(), forKey:"rotateAnimation")

        }else{

            DispatchQueue.main.asyncAfter(deadline: .now()+1.5, execute: {

                self.dismissHud()

            })

        }

        hudView.alpha= 0

        UIView.animate(withDuration: 0.5, animations: {

            hudView.alpha= 1

        })

    }

    class func dismissHud(){

        let view = UIApplication.shared.keyWindow?.viewWithTag(999) as UIView?

        UIView.animate(withDuration: 0.3, animations: {

            view?.alpha= 0

        }, completion: { flagin

            view?.removeFromSuperview()

        })

    }

}

enumShowHudType{

    caseinfo

    casesuccess

    caseerror

    caseloading


    public  funcgetHudView()->UIImage{

        switchself{

        case.info:

            returnUIImage(named:"hud_info")!

        case.success:

            returnUIImage(named:"hud_success")!

        case.error:

            returnUIImage(named:"hud_error")!

        case.loading:

            returnUIImage(named:"hud_loading")!

        }

    }

}

你可能感兴趣的:(2018-08-06 swift 自定义hud)