SPIndicator

模仿静音模式开启/关闭时出现的指示器。提供两种动画预设:doneerror。也支持自定义图像,并可从顶部、中心和底部显示。

如果想要像 Apple Music 中那样的提示,请使用这个库 SPAlert

导航

安装

可在 iOS 12+ 和 tvOS 12+ 上使用。

Swift Package Manager

Swift Package Manager 是一个用于自动化 Swift 代码分发的工具,并已集成到 swift 编译器中。它与 Swift 构建系统集成,可以自动执行下载、编译和链接依赖项的过程。

设置好 Swift 包后,只需将其添加到 Package.swiftdependencies 值中,即可轻松地将其添加为依赖项。

dependencies: [
    .package(url: "https://github.com/ivanvorobei/SPIndicator", .upToNextMajor(from: "1.6.0"))
]

CocoaPods

CocoaPods 是一个依赖管理器。 有关使用和安装说明,请访问其网站。 要使用 CocoaPods 进行集成,请在 Podfile 中指定它

pod 'SPIndicator'

手动

如果您不想使用任何依赖管理器,可以手动集成。 将 Sources/SPIndicator 文件夹放入您的 Xcode 项目中。 确保启用 Copy items if neededCreate groups

快速开始

为了获得最佳体验,我建议通过调用类函数 SPIndicator 来显示指示器。 这些函数会定期更新,并以 Apple 的方式显示指示器

// Presets:
SPIndicator.present(title: "Error", message: "Try Again", preset: .error)

// For show with custom image:
let image = UIImage.init(systemName: "sun.min.fill")!.withTintColor(.systemYellow, renderingMode: .alwaysOriginal)
SPIndicator.present(title: "Custom Image", message: "With tint color", preset: .custom(image))

// For show text only:
SPIndicator.present(title: "Error", haptic: .error)

用法

持续时间

要更改显示持续时间,请创建指示器视图并调用带有自定义持续时间的 present 方法

let indicatorView = SPIndicatorView(title: "Complete", preset: .done)
indicatorView.present(duration: 3)

布局

要自定义布局和边距,请使用 layout 属性。 您可以管理每侧的边距、图标大小以及图像和标题之间的间距

indicatorView.layout.iconSize = .init(width: 24, height: 24)
indicatorView.layout.margins.top = 12

拖动关闭

默认情况下,您可以拖动指示器以隐藏它。 在拖动指示器时,关闭不起作用。 可以禁用此行为

indicatorView.dismissByDrag = false

触感反馈

要管理触感反馈,您应该在 present 方法中传递它

indicatorView.present(duration: 1.5, haptic: .success, completion: nil)

您可以从 init 中删除持续时间和完成参数。 它们具有默认值。

显示位置

您可以更改显示位置

SPIndicator.present(title: "Error", message: "Try Again", preset: .error, from: .bottom)

// or for custom `SPIndicatorView`

indicatorView.presentSide = .bottom

在上述情况下,指示器将从底部出现并附加到底部。 要管理偏移量 - 请检查属性 offset

共享配置

此外,您可以更改某些警报的默认值。 例如,您可以使用此代码更改警报的默认持续时间

SPIndicatorView.appearance().duration = 2

它将应用于所有警报。 我建议在 app delegate 中设置它,但您可以在运行时更改它。

SwiftUI

像系统警报一样,仅显示消息提示

Button("Show Indicator") {
    showIndicator = true
}.SPIndicator(isPresent: $showIndicator, title: "This is title only")

或显示消息、标题、图像和其他配置

Button("Show Indicator") {
    showIndicator = true
}.SPIndicator(
    isPresent: $showIndicator, 
    title: "Title", 
    message: "Message",
    duration: 2.0, 
    presentSide: .top,
    dismissByDrag: false, 
    preset: .custom(UIImage(systemName: "heart")!), 
    haptic: .success, 
    layout: .init(), 
    completion: {
        print("Indicator is destoryed")
    })

俄罗斯社区

Я веду телеграм-канал, там публикую новости и туториалы.
С проблемой помогут в чате.

Видео-туториалы выклыдываю на YouTube

Tutorials on YouTube