美文网首页
自定义加载控件

自定义加载控件

作者: ZhouMac | 来源:发表于2016-06-13 10:50 被阅读33次

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

    相关文章

      网友评论

          本文标题:自定义加载控件

          本文链接:https://www.haomeiwen.com/subject/cxdydttx.html