自定义封装进度提示框《MrLoadView》

封装MrLoadView类: MrLoadView程序源代码下载

          **效果图:**

这里写图片描述

// 关键代码
    /**
     初始化UI布局
     */
    func setUI() {
        if(self.titleLabel != nil) {
            self.titleLabel!.removeFromSuperview()
        }

        // 底部的灰色layer
        let bottomShapeLayer = CAShapeLayer()
        bottomShapeLayer.strokeColor = self.bottomColor.CGColor
        bottomShapeLayer.fillColor = UIColor.clearColor().CGColor
        bottomShapeLayer.lineWidth = MrShapeLayerLineWidth
        // 图形的路径
        bottomShapeLayer.path = UIBezierPath(roundedRect: CGRect(x: MrShapeLayerMargin, y: 0, width: MrShapeLayerWidth, height: MrShapeLayerWidth), cornerRadius: MrShapeLayerRadius).CGPath
        self.layer.addSublayer(bottomShapeLayer)

        // 橘黄色的layer
        let ovalShapeLayer = CAShapeLayer()
        ovalShapeLayer.strokeColor = self.animationColor.CGColor
        ovalShapeLayer.fillColor = UIColor.clearColor().CGColor
        ovalShapeLayer.lineWidth = MrShapeLayerLineWidth
        // 分割点效果
        ovalShapeLayer.lineDashPattern = [6, 3]
        ovalShapeLayer.path = UIBezierPath(roundedRect: CGRect(x: MrShapeLayerMargin, y: 0, width: MrShapeLayerWidth, height: MrShapeLayerWidth), cornerRadius: MrShapeLayerRadius).CGPath
        self.layer.addSublayer(ovalShapeLayer)

        //  起点动画
        let strokeStartAnimation = CABasicAnimation(keyPath: "strokeStart")
        strokeStartAnimation.fromValue = -1
        strokeStartAnimation.toValue = 1.0

        //  终点动画
        let strokeEndAnimation = CABasicAnimation(keyPath: "strokeEnd")
        strokeEndAnimation.fromValue = 0.0
        strokeEndAnimation.toValue = 1.0

        //  组合动画
        let animationGroup = CAAnimationGroup()
        animationGroup.animations = [strokeStartAnimation, strokeEndAnimation]
        animationGroup.duration = (Double)(MrAnimationDurationTime)
        animationGroup.repeatCount = (Float)(CGFloat.max)
        // 设置保存动画的最新状态
        animationGroup.fillMode = kCAFillModeForwards
        // 设置动画执行完毕之后不删除动画
        animationGroup.removedOnCompletion = false

        // 添加核心动画到layer
        ovalShapeLayer.addAnimation(animationGroup, forKey: nil)

        self.titleLabel = UILabel()
        self.titleLabel!.frame = CGRectMake(0, MrShapeLayerWidth + 5, MrLoadingViewWidth + 10, 30)
        self.titleLabel!.numberOfLines = 0
        self.titleLabel!.text = self.message
        self.titleLabel!.textColor = MrLoadingTextColor
        self.titleLabel!.font = UIFont.systemFontOfSize(12)
        self.titleLabel!.textAlignment = NSTextAlignment.Center

        self.addSubview(self.titleLabel!)
    }

    /**
     显示加载动画
     */
    func showLoadingView() {
        if(isShowing == true) {  // 如果没有退出动画, 就不能继续添加
            return
        }

        isShowing = true
        self.addSelfViewToWindow()
    }

    /**
     关闭加载动画
     */
    func dismissLoadingView() {
        if(isShowing == false) {
            return
        }

        isShowing = false
        self.removeFromSuperview()
    }

    /**
     显示加载动画(带毛玻璃效果)
     */
    func showLoadingViewWithBlur() {
        if(isShowing == true) { // 如果没有退出动画,就不能继续添加
            return
        }

        isShowing = true
        self.addSelfViewToWindow()

        // 拿到主窗口
        let window = UIApplication.sharedApplication().keyWindow
        // view的x
        let viewCenterX: CGFloat = CGRectGetWidth(UIScreen.mainScreen().bounds)/2
        // view的Y
        let viewCenterY: CGFloat = CGRectGetHeight(UIScreen.mainScreen().bounds)/2

        // 初始化模糊效果
        blurView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.Light))
        blurView?.layer.cornerRadius = 10
        blurView?.layer.masksToBounds = true
        blurView?.frame = CGRectMake(0, 0, 100, 100)
        blurView?.center = CGPointMake(viewCenterX, viewCenterY)

        /**
        添加毛玻璃效果
        */
        window?.insertSubview(blurView!, belowSubview: self)
    }
    /**
     将本身添加到主窗口
     */
    func addSelfViewToWindow() {
        // 拿到主窗口
        let window = UIApplication.sharedApplication().keyWindow

        // view的x
        let viewCenterX: CGFloat = CGRectGetWidth(UIScreen.mainScreen().bounds)/2
        // view的Y
        let viewCenterY: CGFloat = CGRectGetHeight(UIScreen.mainScreen().bounds)/2

        self.frame = CGRectMake(0, 0, MrLoadingViewWidth, MrLoadingViewWidth)
        self.center = CGPointMake(viewCenterX, viewCenterY)

        // 添加到主窗口中去
        window?.addSubview(self)
    }
}

使用测试:

    func showLoadView(sender: UIButton) {
        let loadView = MrLoadView.sharedInstance
//        loadView.message = "努力加载中..."
//        loadView.animationColor = UIColor.greenColor()
//        loadView.bottomColor = UIColor.grayColor()
        loadView.showLoadingViewWithBlur()
    }

    func dismissLoadView(sender: UIButton) {
        MrLoadView.sharedInstance.dismissLoadingViewWithBlur()
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        MrLoadView.sharedInstance.dismissLoadingViewWithBlur()
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值