如何从 UIVisualEffectView 和 UIActivityIndi​​catorView 创建一个新的 class

How to create a new class from UIVisualEffectView and UIActivityIndicatorView

我制作了一个非常有效的繁忙指示器,只要它在 ViewController 我想在其中显示指示器。我试图将其移至新的 class类型为 UIView,但无法在 ViewController 中显示任何内容。

这是工作代码,不是它自己的类型:

//Create a busy indicator that can be shown by changing a single variable
var blur: UIVisualEffectView?
var spinner: UIActivityIndicatorView?
var showingActivity: Bool = false {
    didSet{
        switch showingActivity {
        case true:
            blur = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.Dark))
            blur!.frame = CGRectMake(100, 100, 150, 150)
            blur!.center = self.view.center
            blur!.layer.cornerRadius = 10
            blur!.clipsToBounds = true
            self.view.addSubview(blur!)

            spinner = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
            spinner!.frame = CGRectMake(0, 0, 50, 50)
            spinner!.center = self.view.center
            spinner!.hidesWhenStopped = true
            spinner!.startAnimating()
            self.view.addSubview(spinner!)

            UIApplication.sharedApplication().networkActivityIndicatorVisible = true //network activity option
        case false:
            blur?.removeFromSuperview()
            spinner?.removeFromSuperview()
            blur = nil
            spinner = nil

            UIApplication.sharedApplication().networkActivityIndicatorVisible = false //network activity option
        default: break
        }
    }
}
func toggleNetworkActivity() {
    showingActivity = !showingActivity
}

这段代码似乎没有创建任何东西。

class busy : UIView {
var blur = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.Dark))
var spinner = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)

init() {
    let frame = CGRect(x: 0, y: 0, width: 300, height: 300)
    super.init(frame: frame)

    blur.frame = CGRectMake(100, 100, 150, 150)
    blur.center = self.center
    blur.layer.cornerRadius = 10
    blur.clipsToBounds = true

    spinner.frame = CGRectMake(0, 0, 50, 50)
    spinner.center = self.center
    spinner.hidesWhenStopped = true
    spinner.startAnimating()

    self.setNeedsDisplay()

}

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

并且在 ViewDidLoad 中:

var test = busy()
test.center = self.view.center
self.view.addSubview(test)

想通了。这是代码,如果有人感兴趣的话。但是,这是执行此类任务的正确方法吗?

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

然后在使用中:

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