自定义加载控件

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")
        }
    }

}

你可能感兴趣的:(自定义加载控件)