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。
仅需两步!
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()
hermes.postNotification(successNotification)
hermes.postNotification(failureNotification)
// 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()
// we could have also done the above code by simply using postNotifications
hermes.postNotifications([successNotification, failureNotification])
就这么简单!
继承 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,或者总是返回您自定义的通知视图。这取决于您!