一个 Swift Toast 视图 - iOS 14 风格 - 使用 UIKit 构建。🍞
你可以使用 Swift 包管理器安装 Toast-Swift,只需将描述添加到你的 Package.swift 文件中即可
dependencies: [
.package(url: "https://github.com/BastiaanJansen/toast-swift", from: "2.1.3")
]
pod "ToastViewSwift"
创建基于简单文本的 toast
let toast = Toast.text("Safari pasted from Notes")
toast.show()
或者添加一个副标题
let toast = Toast.text("Safari pasted from Notes", subtitle: "A few seconds ago")
toast.show()
如果你想使用自己的字体(支持 NSAttributedString)
let attributes = [
NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Bold", size: 17)!,
NSAttributedStringKey.foregroundColor: UIColor.black
]
let attributedString = NSMutableAttributedString(string: "Safari pasted from Notes" , attributes: attributes)
let toast = Toast.text(attributedString)
toast.show()
如果你想添加一个图标,使用 default
方法来构建一个 toast
let toast = Toast.default(
image: UIImage(systemName: "airpodspro")!,
title: "Airpods Pro",
subtitle: "Connected"
)
toast.show()
想使用不同的布局,但仍然使用 Apple 风格? 创建你自己的视图并在创建自定义 toast 时将其注入到 AppleToastView
类中
let customView: UIView = // Custom view
let appleToastView = AppleToastView(child: customView)
let toast = Toast.custom(view: appleToastView)
toast.show()
show
方法接受几个可选参数。 haptic
类型为 UINotificationFeedbackGenerator.FeedbackType
用于使用触觉反馈,after
类型为 TimeInterval
用于在特定时间后显示 toast
toast.show(haptic: .success, after: 1)
text
, default
和 custom
方法支持自定义配置选项。 以下选项可用
名称 | 描述 | 类型 | 默认值 |
---|---|---|---|
direction |
Toast 将显示的位置。 | .bottom 或 .up |
.up |
dismissBy |
选择 toast 何时消失。 | Dismissable |
[.time , .swipe ] |
animationTime |
显示和关闭动画的持续时间,以秒为单位。 | TimeInterval |
0.2 |
enteringAnimation |
Toast 显示时将使用的动画类型 | .slide , .fade , .scaleAndSlide , .scale 和 .custom |
.default |
exitingAnimation |
Toast 退出时将使用的动画类型 | .slide , .fade , .scaleAndSlide , .scale 和 .custom |
.default |
attachTo |
Toast 视图将附加到的视图。 | UIView |
nil |
let config = ToastConfiguration(
direction: .top,
dismissBy: [.time(time: 4.0), .swipe(direction: .natural), .longPress],
animationTime: 0.2
)
let toast = toast.text("Safari pasted from Notes", config: config)
self.toast = Toast.text(
"Safari pasted from Noted",
config: .init(
direction: .bottom,
enteringAnimation: .fade(alphaValue: 0.5),
exitingAnimation: .slide(x: 0, y: 100))
).show()
以上配置将显示一个 toast,该 toast 将以淡入动画出现在屏幕上。 然后在退出时会向下移动并消失。
self.toast = Toast.text(
"Safari pasted from Noted",
config: .init(
direction: .bottom,
enteringAnimation: .scale(scaleX: 0.6, scaleY: 0.6),
exitingAnimation: .default
).show()
以上配置将显示一个 toast,该 toast 将以从 0.6 放大到 1.0 的动画出现在屏幕上。 然后在退出时将使用我们的默认动画(即 scaleAndSlide)
有关动画的更多信息,请参阅 Toast.AnimationType
枚举。
不喜欢默认的 Apple 风格? 没问题,也可以使用 custom
方法使用自定义 toast 视图。 首先,创建一个类,确认符合 ToastView
协议
class CustomToastView : UIView, ToastView {
private let text: String
public init(text: String) {
self.text = text
}
func createView(for toast: Toast) {
// View is added to superview, create and style layout and add constraints
}
}
在 Toast
上使用 custom
构造方法来使用你的自定义视图
let customToastView: ToastView = CustomToastView(text: "Safari pasted from Notes")
let toast = Toast.custom(view: customToastView)
toast.show()
要依次显示 toast,请使用 ToastQueue
类
let toast1 = Toast.text("Notification 1")
let toast2 = Toast.text("Notification 2")
let toast3 = Toast.text("Notification 3")
let queue = ToastQueue([toast1, toast2, toast3])
queue.show()
以下委托函数在实现 ToastDelegate
时是可选的。
extension MyViewController: ToastDelegate {
func willShowToast(_ toast: Toast) {
print("Toast will be shown after this")
}
func didShowToast(_ toast: Toast) {
print("Toast was shown")
}
func willCloseToast(_ toast: Toast) {
print("Toast will be closed after this")
}
func didCloseToast(_ toast: Toast) {
print("Toast was closed (either automatically, dismissed by user or programmatically)")
}
}
Toast-Swift 在 MIT 许可下可用。 更多信息请参见 LICENCE 文件。