import UIKit
class ViewController: UIViewController {
// 首先添加一个环形进度条对象,作为当前视图控制器类属性
var indicator:UIActivityIndicatorView!
var p = true
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//环形进度条控件的使用
self.view.backgroundColor = UIColor.black
// 初始化环形进度条,并设置环形进度条为大白
indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
// 设置环形进度条的中心点位置
indicator.center = CGPoint(x: 160, y: 120)
// 开始进度条动画的播放
indicator.startAnimating()
self.view.addSubview(indicator)
// 创建一个按钮
let button = UIButton(frame: CGRect(x: 20, y: 200, width: 280, height: 44))
button.setTitle("Start/Stop", for: UIControlState())
button.backgroundColor = UIColor.orange
button.layer.masksToBounds = true
button.layer.cornerRadius = 10
button.addTarget(self, action: #selector(ViewController.stopAnimation), for: UIControlEvents.touchUpInside)
self.view.addSubview(button)
}
// 创建一个方法:当点击按钮式停止环形进度条的播放,再次点击则开始播放
@objc func stopAnimation(){
if(p == true){
p = !p
indicator.stopAnimating()
}else{
p = !p
indicator.startAnimating()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}