🍞 Rye

Swift Package Manager Carthage Compatible CocoaPods Compatible Platforms GitHub license

简介

Rye 允许你向用户展示非侵入式的提示。

你可以选择显示默认的 Rye 提示类型,或者完全自定义并显示你自己的 UIView

示例

带有图像的自定义 Rye 提示 带有按钮的自定义 Rye 提示 默认的 Rye 提示

📝 要求

iOS 11.4
Swift 5

📦 安装

Swift Package Manager

复制此仓库 URL,并将其添加到你的 Package Dependencies 中

https://github.com/nodes-ios/Rye.git

Carthage

github "nodes-ios/Rye"

Cocoapods

pod 'Rye'

💻 用法

原则

要显示 Rye 提示,你需要声明一个新的 RyeViewController,然后调用

注意:根据你选择的 dismissMode,你可能不需要自己关闭提示。有关更多信息,请参见下面的 displayModes 部分。

最起码你需要考虑

显示文本

要使用 Rye 提示显示文本,你需要创建一个 RyeConfiguration。这是一个字典,允许你配置 Rye 提示的各种 UI 相关方面。有关可用键的更多信息,请参阅 可能的 Rye 配置值 部分。

你可以添加到 RyeConfiguration 的值之一是要在提示中显示的文本。

let ryeConfiguration: RyeConfiguration = [ Rye.Configuration.Key.text: "Message for the user" ]

提示类型

你可以使用默认的 Rye 提示,也可以创建自己的 UIView 并使用它代替。要确定使用哪个,你需要使用如下定义的 Rye.ViewType 枚举

public enum ViewType {
    case standard(configuration: RyeConfiguration?)
    case custom(UIView, configuration: RyeConfiguration?)
}

如你所见,standardcustom ViewType 都采用一个可选的 RyeConfiguration 作为参数。 这意味着你不必提供 RyeConfiguration,在这种情况下,所有参数(包括文本)都将使用默认值(但你可能不希望看到一个显示文本 “Add a message” 的提示,对吗?)。

此外,custom ViewType 接受你要使用的自定义 UIView。

请注意,某些 RyeConfiguration 键在使用自定义视图时是不相关的。 更具体地说,当你决定为你的消息使用 custom 视图时,以下键不会被使用:

有关 AnimationType 的更多信息,请参阅下面的 动画类型 部分。

在哪里显示提示?

Rye 提示的显示位置由 Rye.PositionRye.Alignment 枚举确定,定义如下:

public enum Position {
    case top(inset: CGFloat)
    case bottom(inset: CGFloat)
}
public enum Alignment {
    case leading(inset: CGFloat)
    case center
    case trailing(inset: CGFloat)
}

如果在初始化时未指定对齐方式,则默认为 .center 对齐。

有关 Rye.PositionRye.Alignment 的更多信息,请参阅下面的 位置 & 对齐 部分。

显示默认的 Rye 提示

遵循这些原则,我们现在可以显示我们的第一个 Rye 提示了。

import Rye
...
let ryeConfiguration: RyeConfiguration = [Rye.Configuration.Key.text: "Message for the user"]
let rye = RyeViewController(
   viewType: .standard(configuration: ryeConfiguration),
   at: .bottom(inset: 16)
)
rye.show()

这将导致一个带有文本 “Message for the user” 的 Rye 提示出现在屏幕底部,并在 2.5 秒后自动消失。

控制关闭类型

如果你希望 Rye 提示以不同的方式消失,你可以在创建 RyeViewController 时传递一个 dismissMode 参数

import Rye
...
let ryeConfiguration: RyeConfiguration = [Rye.Configuration.Key.text: "Message for the user"]
let rye = RyeViewController(
   dismissMode: .gesture,
   viewType: .standard(configuration: ryeConfiguration),
   at: .bottom(inset: 16)
)
rye.show()

现在,提示将停留在屏幕上,直到用户点击或滑动它。

显示带有自定义配置的默认 Rye 提示

如果你想更好地控制提示视图,你可以像下面这样向 RyeConfiguration 字典添加键和值

import Rye
...
let ryeConfiguration: RyeConfiguration = [
    Rye.Configuration.Key.text: "Error message for the user",
    Rye.Configuration.Key.backgroundColor: UIColor.red.withAlphaComponent(0.4),
    Rye.Configuration.Key.animationType: Rye.AnimationType.fadeInOut
]

let rye = RyeViewController(
   viewType: .standard(configuration: ryeConfiguration),
   at: .bottom(inset: 16)
)
rye.show()

使用自定义 UIView 显示 Rye 提示

为了获得更多的控制权,你可以创建自己的 UIView 子类,并将 viewType 参数设置为 .custom

import Rye
...

let customView = YourCustomView()
let rye = RyeViewController(viewType: .custom(customView))

rye.show()

关闭完成

如果你想在 Rye 提示关闭时执行一些代码,你可以在调用 show 时传递一个 dismissCompletion 代码块,如下所示

import Rye
...
let ryeConfiguration: RyeConfiguration = [Rye.Configuration.Key.text: "Message for the user"]
let rye = RyeViewController(
   viewType: .standard(configuration: ryeConfiguration),
   at: .bottom(inset: 16)
)

rye.show(withDismissCompletion: {
    print("Goodbye from Rye, time to dy..die")  
})

手动关闭 Rye 提示

如果你选择将 Rye 提示显示为 .nonDismissable,则必须自己关闭它。 保留对 RyeViewController 的引用,并在准备好释放它时调用 dismiss

import Rye
...
var rye: RyeViewController?

let ryeConfiguration: RyeConfiguration = [Rye.Configuration.Key.text: "Message for the user"]
rye = RyeViewController(
   dismissMode: .nonDismissable,
   viewType: .standard(configuration: ryeConfiguration),
   at: .bottom(inset: 16)
)

rye?.show()

...at a later point in time
rye?.dismiss()

参数描述

下面你可以找到用于控制 Rye 提示的各种参数的描述。

显示模式

Rye 支持三种不同的 displayMode 值,可以在创建新的 RyeViewController 时传递这些值

如果在创建新的 RyeViewController 时未传递此值,则使用 automatic 的默认值,默认间隔为 2.5 秒(默认间隔在 Rye.defaultDismissInterval 中定义)

位置和对齐

你可以指定 Rye 提示应显示在屏幕的顶部还是底部。 这是通过初始化时的 position 参数指定的。 position 参数接受一个关联值,允许你定义一个插图。

默认情况下,Rye 会为你计算安全区域插图,因此请务必仅指定额外的所需插图。

同样,你可以通过 aligned 参数设置 Rye 的对齐方式。 .leading.trailing 对齐方式也采用一个关联值,允许你定义一个插图。 如果在初始化时未指定 aligned 参数,则默认为 .center

动画类型

Rye 提供两种动画类型

要控制动画的持续时间,请使用 RyeConfigurationanimationDuration 键并提供一个 TimeInterval 值。

如果你没有animationDuration 提供值,则使用 0.3 秒的标准值。

忽略安全区域

在确定 Rye 消息的显示位置时,你可以选择是否在计算中包含 safeLayoutArea 插图。

这是通过在 RyeConfiguration 中设置 .ignoreSafeAreas 值来完成的。

默认值为 false,这意味着将在计算中使用安全区域插图。

可能的 Rye 配置值

以下键可以在配置字典中使用

.backgroundColor (must be a UIColor)
.textColor (must be a UIColor)
.textFont (must be a UIFont)
.text (must be a String)
.cornerRadius (must be a CGFloat)
.animationType (must be a Rye.AnimationType)
.animationDuration (must be a TimeInterval)
.ignoreSafeAreas (must be a bool)

如果配置设置为 nil,则将使用默认配置。 任何设置的选项都将覆盖默认状态。

⚠️注意事项

为了显示 Rye 消息,需要一个 parentView 来确定 Rye 消息相对于什么定位。

如果你试图在无法获取 parentView 之前显示 Rye 消息,你将在 IDE 的控制台中看到此警告。

找不到 parentView 来显示 Rye 消息。 你是否试图在视图生命周期准备好显示视图之前显示 Rye 消息?

例如,如果你尝试在 UIViewControllerviewDidLoad() 中调用 RyeViewController 上的 show(),则可以看到这一点。

示例项目

要了解更多信息,请参阅此仓库中包含的 RyeExample 项目。

⬆️ 从 v1.x.x 更新到 v2.0.0

在 Rye 的 2.0.0 版本中,我们更改了显示消息的方式。

不再区分 .toast.snackBar。 相反,现在每条消息都显示在视图堆栈最顶层的单独 UIWindow 中,并且你必须使用前面描述的 displayModes 来决定如何关闭消息。

这也意味着之前的 init 方法:RyeViewController.init(alertType:viewType:at:timeAlive:) 已被弃用。 如果你使用此 init 方法的 2.0.0 版本,你将在编译期间收到弃用警告。

如果你固执地坚持,仍然可以使用现在已旧的 init 方法。 在幕后,Rye 将为你创建一个新的 RyeViewController,并根据以下规则设置 displayMode

如果你添加了一个 timeAlive 值,则 timeAlive 将用于创建一个 displayMode,其值为 .automatic(interval: timeAlive)

如果没有添加 timeAlive 值,则 displayMode 将为 .nonDismissable

👥 鸣谢

Nodes 用 ❤️ 制作。

📄 许可

Rye 在 MIT 许可下可用。 有关更多信息,请参阅 LICENSE 文件。