[Swift通天遁地]一、超级工具-(11)使用EZLoadingActivity制作Loading加载等待动画

本文将演示加载等待动画的制作。

首先确保在项目中已经安装了所需的第三方库。

点击【Podfile】,查看安装配置文件。

1 platform :ios, '8.0'
2 use_frameworks!
3 
4 target 'DemoApp' do
5     source 'https://github.com/CocoaPods/Specs.git'
6     pod 'EZLoadingActivity'
7 end

根据配置文件中的相关配置,安装第三方库。

然后点击打开【DemoApp.xcworkspace】项目文件。

在项目导航区,打开视图控制器的代码文件【ViewController.swift】

现在编写代码,实现加载等待动画的制作。

 1 import UIKit
 2 //在当前的类文件中引入所需的第三方类库
 3 import EZLoadingActivity
 4 
 5 class ViewController: UIViewController {
 6 
 7     override func viewDidLoad() {
 8         super.viewDidLoad()
 9         // Do any additional setup after loading the view, typically from a nib.
10         
11         //初始化一个按钮控件,当用户点击该按钮时,开始播放加载动画。
12         let newOne = UIButton(frame: CGRect(x: 0, y: 60, width: 320, height: 40))
13         //设置按钮对象的背景颜色为橙色
14         newOne.backgroundColor = UIColor.orange
15         //设置按钮控件在正常状态下的标题文字
16         newOne.setTitle("Show Loading Activity", for: .normal)
17         //给按钮控件绑定点击事件
18         newOne.addTarget(self, 
19                          action: #selector(ViewController.showLoadingActivity(_:)),
20                          for: .touchUpInside)
21         
22         //初始化第二个按钮控件,当用户点击该按钮时,结束播放加载动画。
23         let hideOne = UIButton(frame: CGRect(x: 0, y: 120, width: 320, height: 40))
24         //设置按钮对象的背景颜色为橙色
25         hideOne.backgroundColor = UIColor.orange
26         //设置按钮控件在正常状态下的标题文字
27         hideOne.setTitle("Hide Loading Activity", for: .normal)
28         //给按钮控件绑定点击事件
29         hideOne.addTarget(self, 
30                           action: #selector(ViewController.hideLoadingActivity(_:)),
31                           for: .touchUpInside)
32         
33         //将两个按钮控件依次添加到当前视图控制器的根视图
34         self.view.addSubview(newOne)
35         self.view.addSubview(hideOne)
36     }
37     
38     //添加一个方法,用来响应第一个按钮的点击事件,实现开始加载动画。
39     func showLoadingActivity(_ button : UIButton)
40     {
41         //初始化一个加载动画对象,并通过调用显示命令,开始动画的播放。
42         //并且设置了加载动画时显示的等待文字的内容。
43         //let newOne = EZLoadingActivity.show("Loading...", disableUI: true)
44         //print(newOne)
45         
46 
47         //第三方库允许开发者对加载动画,进行一些自定义的设置
48         //设置加载动画的背景颜色为橙色
49         EZLoadingActivity.Settings.BackgroundColor = UIColor.orange
50         //设置加载动画的旋转图标的颜色为白色
51         EZLoadingActivity.Settings.ActivityColor = UIColor.white
52         //设置加载动画的标识文字的颜色为白色
53         EZLoadingActivity.Settings.TextColor = UIColor.white
54         //设置动画完成时的成功图标
55         EZLoadingActivity.Settings.SuccessIcon = ""
56         //调用加载动画的显示方法,开始动画的播放。
57         _ = EZLoadingActivity.show("Loading...", disableUI: false)
58     }
59     
60     //添加一个方法,用来响应第二个按钮的点击事件,实现结束加载动画。
61     func hideLoadingActivity(_ button : UIButton)
62     {
63         //通过加载动画的隐藏功能,结束加载动画的播放
64         //let hideOne = EZLoadingActivity.hide(true, animated: true)
65 
66         //对上一行的代码进行一些修改
67         //修改后的代码,将使加载动画立即结束,而不会另外播放加载成功的动画。
68         let hideOne = EZLoadingActivity.hide()
69         //输出日志
70         print(hideOne)
71     }
72 
73     override func didReceiveMemoryWarning() {
74         super.didReceiveMemoryWarning()
75         // Dispose of any resources that can be recreated.
76     }
77 }

猜你喜欢

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