目錄:[Swift]Xcode實際操做html
本文將演示環形進度條控件的使用。swift
在項目導航區,打開視圖控制器的代碼文件【ViewController.swift】ide
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 }