GTToast 是一个用 Swift 编写的库,它允许显示 toast 通知。您可以使用文本和/或图像创建 toast。有很多选项可供您根据您的应用程序调整 toast 信息的外观和感觉。
您有多种方式可以显示 toast 信息
GTToast.create("your message").show()
这将创建并显示使用默认设置和您的消息的 toast
GTToast.create("your message", image: yourImage).show()
这将创建并显示带有您的消息和左侧显示的图像的 toast
GTToast.create("your message", config: GTToastConfig(), image: yourImage).show()
这将创建并显示带有消息和自定义配置的 toast。您可以在下面找到 GTToastConfig 的所有属性
您可以指定所有 toast 的全局配置
let config: GTToastConfig = GTToastConfig()
let toastFactory: GTToast = GTToast(config: config)
toastFactory.create("your message").show()
toastFactory.create("your message", image: smallImage).show()
可以强制 toast 消失。为了执行此操作,您需要保留 GTToastView 的引用
var toast: GTToastView = GTToast.create("foo bar")
toast.show()
// later when you are ready
toast.dismiss()
您可以使用 GTToastConfig 配置 toast 的外观。以下是您可以指定的所有可能的配置选项列表
允许您指定 toast 的填充。默认:UIEdgeInsets(top: 3.0, left: 3.0, bottom: 3.0, right: 3.0)
toast 的圆角。默认 3.0
用于渲染文本消息的字体。默认:UIFont.systemFontOfSize(12.0)
消息的文本颜色。默认:UIColor.whiteColor()
文本的对齐方式。默认:NSTextAlignment.Center
toast 的背景颜色。默认:UIColor.blackColor().colorWithAlphaComponent(0.8)
在显示和隐藏 toast 时使用的动画类型。默认:GTBottomSlideInAnimation(以下为完整列表)
toast 将显示的时间:默认 4
toast 的底部边距。默认:5
在 toast 中显示的图像的边距。默认:UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
允许您指定图像应该显示的位置(顶部、底部、左侧或右侧)。默认:GTToastAlignment.Left
指定最大图像大小。当图像位于右侧或左侧时,仅考虑宽度属性。类似地,高度仅当图像位于顶部或底部时应用。默认值: CGSize(width: 100, height: 200)
您可以通过创建 GTToastConfig 来指定所有这些内容。
GTToastConfig(
contentInsets: UIEdgeInsets(top:10, left: 9, bottom: 8, right: 7),
cornerRadius: 8.0,
font: UIFont.systemFont(ofSize: 100),
textColor: UIColor.red,
textAlignment: NSTextAlignment.right,
backgroundColor: UIColor.blue.withAlphaComponent(0.8),
animation: GTScaleAnimation(),
displayInterval: 2,
bottomMargin: 15.0,
imageMargins: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10),
imageAlignment: GTToastAlignment.top,
maxImageSize: CGSize(width: 100, height: 100)
)
只需提供您想要更改的属性即可,不需要所有属性。
GTAnimation 是一个协议,描述了动画不同阶段视图的状态。以下是该协议的提供实现:
当您为 GTAnimation
协议提供实现时,必须实现以下三个方法:
func before(view: UIView) -> Void
- 动画开始之前视图的状态。func show(view: UIView) -> Void
- 视图在屏幕上显示时的状态。func hide(view: UIView) -> Void
- 用户关闭视图时的状态。查看当前在 GTAnimation.swift
中的实现。
此控件是用 Swift 3 编写的。您至少需要 Xcode 8。需要将部署目标设置为 8.0 或更高版本。此外,此项目使用 CocoaPods。
使用 CocoaPods 安装 GTToast。在 Podfile 中包含以下内容
use_frameworks!
pod 'GTToast'
在克隆项目后,您需要在 Example 目录中运行 'pod install'。要在 Xcode 中查看,请打开位于 Example 目录中的 GTToast.xcworkspace。
Grzegorz Tatarzyn, [email protected]
GTToast 可在 MIT 许可证下使用。