您好,欢迎来到伴沃教育。
搜索
您的当前位置:首页自定义加载控件

自定义加载控件

来源:伴沃教育

Busy.swift

import UIKit

class Busy : UIView {
    private var blur = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.Dark))
    private var spinner = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
    var isActive: Bool = false
    
    override init (frame : CGRect) {
        super.init(frame : frame)
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("This class does not support NSCoding")
    }
    
    func startActivity() {
        let x = UIScreen.mainScreen().bounds.width/2
        let y = UIScreen.mainScreen().bounds.height/2
        
        blur.frame = CGRectMake(100, 100, 150, 150)
        blur.layer.cornerRadius = 10
        blur.center = CGPoint(x: x, y: y)
        blur.clipsToBounds = true
        
        spinner.frame = CGRectMake(0, 0, 50, 50)
        spinner.hidden = false
        spinner.center = CGPoint(x: x, y: y)
        spinner.startAnimating()
        
        super.addSubview(blur)
        super.addSubview(spinner)
        isActive = true
    }
    
    func stopActivity() {
        blur.removeFromSuperview()
        spinner.removeFromSuperview()
        isActive = false
    }
}


ViewController.swift

import UIKit

class ViewController: UIViewController {
    var test:Busy!

    override func viewDidLoad() {
        super.viewDidLoad()
        test=Busy()
    }

    @IBAction func toggle(sender: AnyObject) {
        if test.isActive {
            test.stopActivity()
            test.removeFromSuperview()
            print("Stopping")
        } else {
            test.startActivity()
            self.view.addSubview(test)
            print("Starting")
        }
    }

}

Copyright © 2019- bangwoyixia.com 版权所有 湘ICP备2023022004号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务