IOS控制元件-環形進度條UIActivityIndicatorView
阿新 • • 發佈:2018-12-21
//新增一個環形進度條物件 作為當前檢視控制器的屬性
var indication:UIActivityIndicatorView!
初始化控制元件
self.view.backgroundColor=UIColor.purple //初始化進度條 設定顏色 indication=UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge) //設定中心點的位置 indication.center=CGPoint(x: 160, y: 120) //開始進度條動畫播放 indication.startAnimating() self.view.addSubview(indication)
設定一個按鈕。點選按鈕結束動畫
let button = UIButton(frame: CGRect(x: 20, y: 200, width: 288, height: 44)) button.setTitle("stop", for: UIControlState()) //繫結事件點選按鈕關閉 動畫 button.addTarget(self, action: #selector(ViewController.stopIndication), for: UIControlEvents.touchUpInside) button.backgroundColor=UIColor.brown self.view.addSubview(button)
按鈕繫結的事件
@objc func stopIndication(){
indication.stopAnimating()
}
程式碼:
class ViewController: UIViewController { //新增一個環形進度條物件 作為當前檢視控制器的屬性 var indication:UIActivityIndicatorView! override func viewDidLoad() { super.viewDidLoad(); // 環形進度條 self.view.backgroundColor=UIColor.purple //初始化進度條 設定顏色 indication=UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge) //設定中心點的位置 indication.center=CGPoint(x: 160, y: 120) //開始進度條動畫播放 indication.startAnimating() self.view.addSubview(indication) let button = UIButton(frame: CGRect(x: 20, y: 200, width: 288, height: 44)) button.setTitle("stop", for: UIControlState()) //繫結事件點選按鈕關閉 動畫 button.addTarget(self, action: #selector(ViewController.stopIndication), for: UIControlEvents.touchUpInside) button.backgroundColor=UIColor.brown self.view.addSubview(button) } @objc func stopIndication(){ indication.stopAnimating() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }