Swift

Flash 消息 -- 或者在 Android 术语中称为 toasts -- 是一种离散的、非模态的提示,旨在通知用户,而不会完全占据他们的注意力。例如,您可以使用 flash 消息来告知用户新数据已完成加载、文档已保存或发生了非严重错误。

Flash.swift 使显示这些类型的消息变得容易,并为您提供自定义其外观和行为的灵活性。

截图

Screensshots

安装

Flash.swift 可通过 Swift Package Manager 获得。要将 Flash.swift 与 SPM 一起使用,请添加 https://github.com/conmulligan/Flash.swift.git 作为依赖项。

入门

有关您可以构建和运行的交互式示例,请查看 Example 目录中的 Xcode 项目。

基本示例

显示 flash 消息可以像创建 FlashView 实例并调用 show() 函数一样简单

let flash = FlashView(text: "Hello!")
flash.show()

您还可以传递一个图像

let star = UIImage(systemName: "star.fill")
let flash = FlashView(text: "Hello!", image: star)
flash.show()

默认情况下,flash 视图直接添加到活动窗口的视图层级结构中。 将 flash 视图直接添加到窗口具有一些优势; 例如,这样做允许 flash 视图在视图层级结构发生变化时仍然存在,例如从导航堆栈中弹出视图控制器。

如果要在特定视图中显示 flash 消息,请将 UIView 实例传递给 show() 方法

flash.show(in: view)

默认情况下,flash 消息可见 2 秒。 您可以通过传递一个 TimeInterval 值来更改持续时间

flash.show(duration: 5) // seconds

如果您希望 flash 消息无限期地显示,请传递持续时间 0

flash.show(duration: 0)

flash 消息将可见,直到用户点击它(如果启用了点击关闭),或者直到以编程方式将其关闭

flash.hide()

自定义

FlashView.Configuration 结构允许您自定义 flash 视图的对齐方式、背景颜色、文本颜色等等。在大多数情况下,应将默认配置用作基线

var configuration = FlashView.Configuration.defaultConfiguration()
configuration.titleProperties.textColor = .red

您还可以使用默认的初始化器从头开始创建一个配置

let configuration = FlashView.Configuration(alignment: .bottom,
                                            playsHaptics: false,
                                            tapToDismiss: false)

创建配置后,将其传递给 flash 视图的初始化器

let flash = FlashView(text: "Hello!", configuration: configuration)
flash.show()

如果要更改所有 flash 视图的配置,请使用 shared 静态属性

FlashView.Configuration.shared.titleProperties.textColor = .white
FlashView.Configuration.shared.backgroundProperties.color = .systemBlue

配置属性

属性 描述
alignment flash 视图的垂直对齐方式。
spacing 图像-文本间距。
insets flash 视图相对于其父视图的布局插图。
contentInsets flash 视图的内部内容插图。
backgroundProperties 背景属性。 使用此选项可以更改背景颜色和圆角半径。
imageProperties 图像视图属性。 使用此选项可以更改图像着色颜色。
titleProperties 标题文本属性。 使用此选项可以更改文本颜色、字体和行数。
playsHaptics 一个布尔值,用于确定 flash 视图在显示时是否播放触觉反馈。
tapToDismiss 一个布尔值,用于确定在点击时是否关闭 flash 视图。
appliesAdditionalInsetsAutomatically 一个布尔值,用于确定 flash 视图是否尝试自动插入以避免与导航 UI 重叠。
animator flash 视图动画器。 使用此选项可以自定义 DefaultAnimator,或提供自定义的 FlashAnimator 类型。

动画

您可以使用 DefaultAnimator.Configuration 结构自定义 flash 视图动画

var configuration = DefaultAnimator.Configuration.defaultConfiguration()
configuration.duration = 0.5 
configuration.scaleCoefficient = 0.75

let animator = DefaultAnimator(configuration: configuration)
FlashView.Configuration.shared.animator = animator

为了更好地控制动画,请创建一个符合 FlashAnimator 的自定义类型。

配置属性

属性 描述
duration 动画持续时间(以秒为单位)。
dampingRatio 在动画进入时应用的弹簧阻尼比。
initialVelocity 在动画进入时 flash 视图的初始速度。
translateAmount 视图在动画时沿 y 轴平移的距离(以点为单位)。
scaleCoefficient 在动画进入和退出时缩放视图的量,介于 0 和 1 之间。 值为 1 不会缩放视图。

SwiftUI

Flash.swift 尚未公开原生 SwiftUI 接口,但您可以将 flash 消息显示为副作用

struct ExampleView: View {
    private func showFlash() {
        let flash = FlashView(text: "Hello!")
        flash.show()
    }

    var body: some View {
        Button("Show Flash") {
            showFlash()
        }
    }
}

许可证

Flash.swift 在 MIT 许可证下可用。 有关更多信息,请参见 LICENSE 文件。