简体   繁体   中英

How to create a new class from UIVisualEffectView and UIActivityIndicatorView

I've made a busy indicator that works really well, so long as it's in the ViewController I want to display the indicator in. I attempted to move this over to a new class of type UIView, but can't get anything to appear in the ViewController.

This is the working code, that is not it's own type:

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

This is the code that doesn't seem to create anything.

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

And in ViewDidLoad:

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

Figured it out. Here is the code, if anyone is interested. But, is this the right way to do this kind of task?

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

And then in use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM