1. 程式人生 > >[Xcode10 實際操作]四、常用控制元件-(12)環形進度條控制元件的使用

[Xcode10 實際操作]四、常用控制元件-(12)環形進度條控制元件的使用

本文將演示環形進度條控制元件的使用。

在專案導航區,開啟檢視控制器的程式碼檔案【ViewController.swift】

 1 import UIKit
 2 
 3 class ViewController: UIViewController {
 4 
 5     //首先新增一個環形進度條物件
 6     var indicator:UIActivityIndicatorView!
 7     
 8     override func viewDidLoad() {
 9         super.viewDidLoad()
10         // Do any additional setup after loading the view, typically from a nib.
11 //設定根檢視的背景顏色為紫色 12 self.view.backgroundColor = UIColor.purple 13 14 //初始化環形進度條,並設定環形進度條的樣式為大白 15 indicator = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.whiteLarge) 16 //設定環形進度條中心點的位置為(160,120) 17 indicator.center = CGPoint(x: 160
, y: 120) 18 //開始進度條動畫的播放 19 indicator.startAnimating() 20 21 //將環形進度條,新增到當前檢視控制器的根檢視 22 self.view.addSubview(indicator) 23 24 //建立一個位置在(20,200),尺寸為(280,44)的按鈕物件 25 let button = UIButton(frame: CGRect(x: 20, y: 200, width: 280, height: 44))
26 //然後設定按鈕的標題文字 27 button.setTitle("Stop", for: .normal) 28 //設定按鈕的背景顏色為橙色 29 button.backgroundColor = UIColor.orange 30 //接著給按鈕物件,繫結點選事件 31 button.addTarget(self, action: #selector(ViewController.stopAnimation(_:)), for: UIControl.Event.touchUpInside) 32 33 //將按鈕物件,新增到檢視控制器的根檢視 34 self.view.addSubview(button) 35 } 36 37 //建立一個方法,用來響應按鈕的點選事件 38 @objc func stopAnimation(_ button:UIButton) 39 { 40 //當點選按鈕時, 41 //停止環形進度條的動畫播放 42 indicator.stopAnimating() 43 } 44 45 override func didReceiveMemoryWarning() { 46 super.didReceiveMemoryWarning() 47 // Dispose of any resources that can be recreated. 48 } 49 }