Toast-Swift
Swift Toast 控件 - iOS 14 风格 - 使用 UIKit 构建。🍞
安装
Swift 包管理器
您可以使用 Swift 包管理器通过在 Package.swift 文件中添加描述来安装 Toast-Swift
dependencies: [
.package(url: "https://github.com/BastiaanJansen/toast-swift", from: "1.4.0")
]
CocoaPods
pod "ToastViewSwift"
使用方法
创建一个简单的文本式弹窗
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
方法构造弹出提示框
let toast = Toast.default(
image: UIImage(systemName: "airpodspro")!,
title: "Airpods Pro",
subtitle: "Connected"
)
toast.show()
想使用不同的布局,但仍然使用苹果风格?创建自己的视图并在创建自定义弹出提示框时将其注入到AppleToastView
类中
let customView: UIView = // Custom view
let appleToastView = AppleToastView(child: customView)
let toast = Toast.custom(view: appleToastView)
toast.show()
show
方法接受几个可选参数。类型为UINotificationFeedbackGenerator.FeedbackType
的haptic
用于使用触觉反馈,以及类型为TimeInterval
的after
在一段时间后将弹出显示
toast.show(haptic: .success, after: 1)
配置选项
text
、default
和custom
方法支持自定义配置选项。以下是一些可用的选项:
名称 | 描述 | 类型 | 默认值 |
---|---|---|---|
direction |
弹出显示的位置。 | .bottom 或.up |
|
autoHide |
当设置为true时,弹窗将在显示时间过后自动关闭。 | 布尔型 |
|
enablePanToClose |
当设置为true时,可以通过向上轻扫来关闭弹窗。 | 布尔型 |
|
displayTime |
弹出显示时间后自动关闭前的持续时间(以秒为单位)。 | 时间间隔 |
4 |
animationTime |
显示和关闭动画的持续时间(以秒为单位)。 | 时间间隔 |
0.2 |
enteringAnimation |
弹出显示时将使用的动画类型。 | .slide 、.fade 、.scaleAndSlide 、.scale 和.custom |
|
exitingAnimation |
退出时使用的动画类型。 | .slide 、.fade 、.scaleAndSlide 、.scale 和.custom |
|
attachTo |
弹出视图将附加到的视图。 | UIView |
nil |
let config = ToastConfiguration(
direction: .top,
autoHide: true,
enablePanToClose: true,
displayTime: 5,
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()
上述配置将显示一个带有淡入动画的弹出提示框。然后当弹出提示框退出时将向下消失。
self.toast = Toast.text(
"Safari pasted from Noted",
config: .init(
direction: .bottom,
enteringAnimation: .scale(scaleX: 0.6, scaleY: 0.6),
exitingAnimation: .default
).show()
上述配置将显示一个从0.6到1.0放大动画弹出的Toast,退出时将使用我们的默认动画(scaleAndSlide)。
有关动画的更多信息,请参阅 Toast.AnimationType
枚举。
自定义Toast视图
不喜欢默认的苹果风风格?没问题,您也可以使用 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()
代理
以下代理函数是在实现 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中查看更多信息。