[Swift通天遁地]九、拔剑吧-(8)创建气泡式页面切换效果

本文将演示使用第三方类库,创建页面之间的气泡式切换效果。

首先确保已经安装了所需的第三方类库。双击查看安装配置文件【Podfile】

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

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

安装完成之后,双击打开项目文件【DemoApp.xcodeproj】

首先创建一个自定义的视图控制器,以实现两个页面之间的跳转。

在项目文件夹上点击鼠标右键,弹出右键菜单。

【New File->【Cocoa Touch->【Next】->

【Class】:AnimationController

【Subclass of:UIViewController

【Language】:Swift

->Next->【Create】

点击打开【AnimationController.swift】,

现在开始编写代码,创建视图控制器的界面。

 1 import UIKit
 2 
 3 class AnimationController: UIViewController {
 4 
 5     override func viewDidLoad() {
 6         super.viewDidLoad()
 7         // Do any additional setup after loading the view, typically from a nib.
 8         
 9         //添加一个按钮,当按钮点击该按钮时,关闭被打开的视图控制器。
10         let button = UIButton(frame: CGRect(x: 130, y: 80, width: 60, height: 60))
11         //设置按钮的背景颜色为白色
12         button.backgroundColor = UIColor.white
13         //通过将圆角半径设置为尺寸的一半,从而创建一个圆形按钮。
14         button.layer.cornerRadius = 30
15         //设置按钮在正常状态下的前景文字。
16         button.setTitleColor(UIColor.orange, for:.normal)
17         //设置按钮在正常状态下的标题文字。
18         button.setTitle("X", for: .normal)
19         //设置按钮标题的字体属性。
20         button.titleLabel?.font = UIFont(name: "Arial", size: 28)
21         //给按钮控件绑定点击事件。
22         button.addTarget(self, action: #selector(AnimationController.dismissViewController(_:)), for: .touchUpInside)
23         
24         //将按钮控件添加到根视图
25         self.view.addSubview(button)
26     }
27     
28     //添加一个方法,用来响应按钮的点击事件。
29     @objc func dismissViewController(_ btn:UIButton)
30     {
31         //当用户点击该按钮时,关闭当前的视图控制器。
32         self.dismiss(animated: true, completion: nil)
33     }
34     
35     override func didReceiveMemoryWarning() {
36         super.didReceiveMemoryWarning()
37         // Dispose of any resources that can be recreated.
38     }
39 }

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

现在开始编写代码,实现气泡式的页面切换效果。

 1 import UIKit
 2 //引入已经安装的第三方类库
 3 import BubbleTransition
 4 
 5 //使当前的类,遵循视图控制器的页面切换代理协议。
 6 class ViewController: UIViewController, UIViewControllerTransitioningDelegate {
 7 
 8     //添加一个按钮,作为当前类的一个属性。
 9     //当用户点击该按钮时,以气泡方式打开另一个视图控制器。
10     var button : UIButton!
11     //初始化一个气泡切换对象。
12     var transition = BubbleTransition()
13     override func viewDidLoad() {
14         super.viewDidLoad()
15         // Do any additional setup after loading the view, typically from a nib.
16         
17         //对按钮进行初始化操作,设置按钮的显示区域。
18         button = UIButton(frame: CGRect(x: 130, y: 400, width: 60, height: 60))
19         //设置按钮的背景颜色为橙色。
20         button.backgroundColor = UIColor.orange
21         //设置按钮在正常状态下的标题文字。
22         button.setTitle("Open", for: .normal)
23         //通过将圆角半径设置为尺寸的一半,从而创建另一个圆形按钮。
24         button.layer.cornerRadius = 30
25         
26         //给按钮绑定点击事件
27         button.addTarget(self, action: #selector(ViewController.popViewController(_:)), for: .touchUpInside)
28         //最后将按钮添加到根视图。
29         self.view.addSubview(button)
30     }
31     
32     //添加一个方法,用来响应按钮的点击事件。
33     @objc func popViewController(_ btn:UIButton)
34     {
35         //初始化刚刚创建的视图控制器。
36         let vc = AnimationController()
37         //设置该视图控制器的切换代理 ,为当前的视图控制器。
38         vc.transitioningDelegate = self
39         //设置视图控制器的切换方式为自定义
40         vc.modalPresentationStyle = .custom
41         //在当前的视图控制器,打开另一个视图控制器。
42         self.present(vc, animated: true, completion: nil)
43     }
44     
45     //添加一个代理方法,用来监听视图控制器被打开的事件。
46     public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?
47     {
48         //设置视图控制器的切换模式为展示模式。
49         transition.transitionMode = .present
50         //设置气泡的起点位置,为按钮的中心点
51         transition.startingPoint = button.center
52         //设置气泡的填充颜色为按钮的背景颜色
53         transition.bubbleColor = button.backgroundColor!
54         
55         //返回设置好的切换对象。
56         return transition
57     }
58     
59     //添加一个方法,用来监听视图控制器被关闭的事件。
60     public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
61     {
62         //设置视图控制器的切换模式为消失模式
63         transition.transitionMode = .dismiss
64         //设置消失气泡的起点位置,为按钮控件的中心点。
65         transition.startingPoint = button.center
66         //设置气泡的填充颜色,为按钮控件的背景颜色。
67         transition.bubbleColor = button.backgroundColor!
68         
69         //返回设置好的切换工作
70         return transition
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/10357329.html