惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tor Project blog
B
Blog RSS Feed
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
H
Hackread – Cybersecurity News, Data Breaches, AI and More
罗磊的独立博客
GbyAI
GbyAI
N
Netflix TechBlog - Medium
博客园 - 司徒正美
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
宝玉的分享
宝玉的分享
W
WeLiveSecurity
Stack Overflow Blog
Stack Overflow Blog
Y
Y Combinator Blog
SecWiki News
SecWiki News
V
Vulnerabilities – Threatpost
Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Register - Security
The Register - Security
Cisco Talos Blog
Cisco Talos Blog
Martin Fowler
Martin Fowler
A
About on SuperTechFans
S
Security @ Cisco Blogs
T
Tenable Blog
C
Check Point Blog
N
News and Events Feed by Topic
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Attack and Defense Labs
Attack and Defense Labs
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cisco Blogs
P
Palo Alto Networks Blog
V
V2EX
博客园 - 聂微东
Project Zero
Project Zero
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Docker
N
News | PayPal Newsroom
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
小众软件
小众软件
Application and Cybersecurity Blog
Application and Cybersecurity Blog
人人都是产品经理
人人都是产品经理
V2EX - 技术
V2EX - 技术
I
Intezer
L
LINUX DO - 最新话题

壹拾肆

2024 年做过的一些梦 2023 年做过的一些梦 2023 年终总结丨随时开始奔跑的心气 2022 年终总结丨惠存这一年 翻译:Why use dependency injection 是时候维护一个属于自己的开源库了 iOS 端豆瓣开源许可提到的库 推荐几个播客节目 iOS 自动化打包上传 AppleStore、fir.im,并发邮件通知测试人员 业余剪辑自我修养 Swift - 权限请求封装 iOS - HMAC 加密算法和一个 MD5 加密的问题 iOS - 一些蓝牙问题的解决 被光放弃的人 OC - 多代理的实现 我的 AIO-notion 工作流搭建 解决使用 GitPage 重定向多次问题 Swift - ?和!的学习 《文学回忆录》读书笔记
Swift - 顶部弹出框封装
Kaaaaai · 2020-08-04 · via 壹拾肆
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115








import UIKit

@objc public enum MessageStyle: Int{
case none
case success
case error
case warning
}


@objc extension KKHeadMessageView{
static var mv: KKHeadMessageView?

@objc public class func showMessageView(_ message: String) -> (){
if mv != nil {
mv?.removeFromSuperview()
mv = nil
}

mv = KKHeadMessageView.init(message: message, style: .warning)
mv!.show()
}

@objc public class func showMessageView(_ message: String, style:MessageStyle) -> (){
if mv != nil {
mv?.removeFromSuperview()
mv = nil
}

mv = KKHeadMessageView.init(message: message, style: style)
mv!.show()
}
}

@objc public class KKHeadMessageView: UIView {

private let message: String
private var label_mes = UILabel.init()

private var style: MessageStyle = .none

private static let successBackgroundColor: UIColor = UIColor(red: 86.0/255, green: 188/255, blue: 138.0/255, alpha: 1)
private static let warningBackgroundColor: UIColor = UIColor(red: 242.0/255, green: 153.0/255, blue: 46.0/255, alpha: 1)
private static let errorBackgroundColor: UIColor = UIColor(red: 192.0/255, green: 36.0/255, blue: 37.0/255, alpha: 1)
private static let noneBackgroundColor: UIColor = UIColor(red: 44.0/255, green: 187.0/255, blue: 255.0/255, alpha: 1)

private init(message: String, style: MessageStyle){
self.message = message
self.style = style

super.init(frame: CGRect(x: 0, y: -49, width: UIScreen.main.bounds.size.width, height: 48))

switch style {
case .success:
self.backgroundColor = KKHeadMessageView.successBackgroundColor
break
case .warning:
self.backgroundColor = KKHeadMessageView.warningBackgroundColor
break
case .error:
self.backgroundColor = KKHeadMessageView.errorBackgroundColor
break
case .none:
self.backgroundColor = KKHeadMessageView.noneBackgroundColor
break
}

let attributes = [NSAttributedString.Key.font : UIFont.init(name: "PingFangSC-Medium", size: 14)]

let textSize = self.message.boundingRect(with: CGSize(width: UIScreen.main.bounds.size.width, height: CGFloat.greatestFiniteMagnitude), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attributes as [NSAttributedString.Key : Any], context: nil).size

let label_mes = UILabel.init(frame: CGRect(x: 10, y: self.frame.height - textSize.height - 5, width: textSize.width, height: textSize.height))
label_mes.font = UIFont.init(name: "PingFangSC-Medium", size: 14)
label_mes.textColor = .white
label_mes.text = message
self.label_mes = label_mes
self.addSubview(self.label_mes)
}

func show(){
UIApplication.shared.keyWindow?.addSubview(self)

let animations : () -> () = {
self.frame.origin.y = -4
}

let completionAnimations : (Bool) -> () = { finished in
if finished {
UIView.animate(withDuration: 5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 5, options: [.curveEaseInOut], animations: {
self.frame.origin.y = -49
}){finished in
self.removeFromSuperview()
}
}
}

UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 10, initialSpringVelocity: 10, options: [.curveEaseInOut], animations: animations, completion: completionAnimations)

}




required public init?(coder aDecoder: NSCoder) {
fatalError("NSCoding not supported. Use init(text, preferences, delegate) instead!")
}
}

如上文所见,基本逻辑相对简单,首先编写一个继承自 UIView 的类,重写 init 方法用来自定义这个 view,为了方便调用,用拓展写了两个类方法 showMessageView(),让后通过定义的枚举来展示不同风格的提示框。虽然作为一个自定义 View,上面的代码可能不是最优写法,但勉强算最适合我当前项目的写法。

展开来说,自定义 View 的官方文档,其实就是去看看 UIButtonUIView 官方是怎么写的。但因为苹果是不开源的,看不到——虽然网上也有反推的实现,但毕竟有作者个人风格,而且有些还是用 C++ 实现的,要模仿着写比较费劲。所以这条先搁置。

在封装消息提示框的时候我也考虑过对外开放的接口做成 MBProgressHUD 中调用方法返回一个单例,或者是和 SwiftMessages 将视图定义和使用分开。但最终因为图省事就选了一个简单的方式。另外还想到一个把 MessageView 用枚举来定义,然后通过实现关联值方法来直接显示,如果换这种实现的话,上面的调用方式可能就会变成这样: