Hermes 1.0.0

Hermes 1.0.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布日期最新版本发布日期2015年7月
SPM支持 SPM

JP McGlone 维护。



Hermes 1.0.0

  • 作者:
  • JP McGlone

hermes logo

hermes text

Hermes 是一个简单且健壮的 iOS 内部通知系统,用 Swift 编写。 它支持以样式或无样式文本、图标、声音、颜色和操作闭包发布通知。您可以轻松地构建自己的通知模板,并将任意数量的属性和功能添加到 HermesNotification 中。

Hermes 会一次性显示所有排队的通知,并提供了轻松滑动查看的通知方式(如果没有触摸任何通知 3 秒钟,它将自动动画显示)

安装

使用

Swift 中导入

import Hermes

Objective-C

#import <Hermes/Hermes.h>

入门指南

组件

  • Hermes (公共)

    您将使用 Hermes.sharedInstance 发布通知。您可以告诉 Hermes 当何时 wait() 收集通知,何时 go() 发布通知。

  • Notification (公共,可扩展)

    通知是一个具有文本、图像、声音和颜色等属性的模型。您可以扩展或子类化通知并在自定义 NotificationViews 中使用它们。

  • NotificationView (公共,可扩展)

    NotificationView 是用于显示通知的 UIView。Hermes 允许您子类化和显示与您的应用程序风格相匹配的自定义 NotificationViews。

  • BulletinView (受保护的)

    BulletinView 是显示 1 个或多个 NotificationViews 的 UIView。Hermes 不允许您子类化和使用您自己的 BulletinView。

如何使用

仅需两步!

  1. 创建一个或多个通知
  2. hermes.postNotifications([...])

示例代码

创建通知

// uses Hermes singleton
let hermes = Hermes.sharedInstance

// 'Upload Complete!' success Notification
let successNotification = HermesNotification()
successNotification.text = "Upload complete!"
successNotification.image = UIImage(named: "success_icon")
successNotification.color = .greenColor()

// Call self.foo() when the NotificationView for this Notification is tapped
successNotification.action = { notification in
    self.foo() 
}

// 'Upload failed :(' failure Notification
let failureNotification = HermesNotification()
failureNotification.text = "Upload failed :("
failureNotification.image = UIImage(named: "error_icon")
failureNotification.color = .redColor()

发布通知

如果您在Bulletin已显示的情况下发布通知,它将在当前Bulletin关闭后显示所有新通知
hermes.postNotification(successNotification)
hermes.postNotification(failureNotification)
您可以让 Hermes 等待并执行,在显示之前收集一批通知
// we could do use wait(), which tells Hermes to collect notifications without showing them yet
hermes.wait()

// post both the success and failure notification 
hermes.postNotification(successNotification)
hermes.postNotification(failureNotification)

// this tells Hermes to post all of the notifications in the queue
hermes.go()
或者,您可以发布一系列 Hermes 会立即显示的通知
// we could have also done the above code by simply using postNotifications
hermes.postNotifications([successNotification, failureNotification])

就这么简单!

hermes success notification

继承 HermesNotificationView

继承 HermesNotificationView 非常简单,您有充分的自由将视图设计得既简单又复杂。

class CustomNotificationView: HermesNotificationView {
  override var notification: HermesNotification? {
    didSet {
      // update your view
    }
  }
}

接下来您需要实现如下 HermesDelegate

hermes.delegate = self

及委托方法

func hermesNotificationViewForNotification(#hermes: Hermes, notification: HermesNotification) -> HermesNotificationView? {
        if notification.tag == kCustomNotificationTag {
            return CustomNotificationView()
        }
        return nil
    }

您可以对 HermesNotifications 进行标记,以便确定使用哪种自定义 HermesNotificationView,或者总是返回您自定义的通知视图。这取决于您!