[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 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10016930.html