// 实例化 let activityview = UIActivityIndicatorView(frame: CGRectMake(10.0,10.0,50.0,50.0)) self.view.addSubview(activityview) activityview.backgroundColor = UIColor.yellowColor() activityview.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge activityview.color = UIColor.redColor() // 停止转圈圈时,隐藏 activityview.hidesWhenStopped = true activityview.startAnimating() activityview.tag = 1000
let swithch = UISwitch(frame: CGRectMake(100.0,0.0,0.0)) self.view.addSubview(swithch) swithch.on = false swithch.addTarget(self,action: Selector("switchValueChange:"),forControlEvents: UIControlEvents.ValueChanged)
// MARK: - switchValueChange func switchValueChange(sender:UISwitch) { let activityview = self.view.viewWithTag(1000) as! UIActivityIndicatorView let openStatus = sender.on if openStatus { if !activityview.isAnimating() { activityview.startAnimating() } } else { activityview.stopAnimating() } }