GTToast 0.3.0

GTToast 0.3.0

测试已测试
语言语言 SwiftSwift
许可 MIT
发布上次发布2016年10月
SPM支持 SPM

Grzegorz Tatarzyn 维护。



GTToast 0.3.0

GTToast

GTToast 是一个用 Swift 编写的库,它允许显示 toast 通知。您可以使用文本和/或图像创建 toast。有很多选项可供您根据您的应用程序调整 toast 信息的外观和感觉。

Preview

使用方法

您有多种方式可以显示 toast 信息

  • 简单 toast
GTToast.create("your message").show()

这将创建并显示使用默认设置和您的消息的 toast

  • 带图像的简单 toast
GTToast.create("your message", image: yourImage).show()

这将创建并显示带有您的消息和左侧显示的图像的 toast

  • 自定义 toast
GTToast.create("your message", config: GTToastConfig(), image: yourImage).show()

这将创建并显示带有消息和自定义配置的 toast。您可以在下面找到 GTToastConfig 的所有属性

  • 具有相同配置的所有 toast

您可以指定所有 toast 的全局配置

let config: GTToastConfig = GTToastConfig()
let toastFactory: GTToast = GTToast(config: config)
toastFactory.create("your message").show()
toastFactory.create("your message", image: smallImage).show()
  • 消失 toast

可以强制 toast 消失。为了执行此操作,您需要保留 GTToastView 的引用

var toast: GTToastView = GTToast.create("foo bar")
toast.show()

// later when you are ready
toast.dismiss()

配置

您可以使用 GTToastConfig 配置 toast 的外观。以下是您可以指定的所有可能的配置选项列表

  • contentInsets: UIEdgeInsets

允许您指定 toast 的填充。默认:UIEdgeInsets(top: 3.0, left: 3.0, bottom: 3.0, right: 3.0)

  • cornerRadius: CGFloat

toast 的圆角。默认 3.0

  • font: UIFont

用于渲染文本消息的字体。默认:UIFont.systemFontOfSize(12.0)

  • textColor: UIColor

消息的文本颜色。默认:UIColor.whiteColor()

  • textAlignment: NSTextAlignment

文本的对齐方式。默认:NSTextAlignment.Center

  • backgroundColor: UIColor

toast 的背景颜色。默认:UIColor.blackColor().colorWithAlphaComponent(0.8)

  • animation: GTAnimation

在显示和隐藏 toast 时使用的动画类型。默认:GTBottomSlideInAnimation(以下为完整列表)

  • displayInterval: NSTimeInterval

toast 将显示的时间:默认 4

  • bottomMargin: CGFloat

toast 的底部边距。默认:5

  • imageMargins: UIEdgeInsets

在 toast 中显示的图像的边距。默认:UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

  • imageAlignment: GTToastAlignment

允许您指定图像应该显示的位置(顶部、底部、左侧或右侧)。默认:GTToastAlignment.Left

  • maxImageSize: CGSize

指定最大图像大小。当图像位于右侧或左侧时,仅考虑宽度属性。类似地,高度仅当图像位于顶部或底部时应用。默认值: 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 是一个协议,描述了动画不同阶段视图的状态。以下是该协议的提供实现:

  • GTAlphaAnimation
  • GTBottomSlideInAnimation
  • GTLeftSlideInAnimation
  • GTRightSlideInAnimation
  • GTScaleAnimation
  • GTLeftInRightOutAnimation
  • GTRightInLeftOutAnimation
  • GTNoAnimation

提供您自己的动画

当您为 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 许可证下使用。