Mercury 1.1

Mercury 1.1

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布时间最后发布2015 年 9 月
SPM支持 SPM

chayelChayel祂森 维护。



  • 作者
  • Chayel祂森

Mercury

Mercury 是一个由 Swift 编写的简单而强大的 iOS 应用内通知系统。 它支持带有或没有样式的文本、图标、声音、颜色和操作闭包的 Notifications。你可以轻松构建自己的通知模板,并将任何数量属性和功能添加到 MercuryNotification。

Mercury 会立即显示所有排队等待的通知,并通过滑动轻松浏览(如果你3秒钟内没有触摸任何通知,它将自动播放动画)

安装

使用

Swift 中导入

import Mercury

Objective-C

#import <Mercury/Mercury.h>

入门

组件

  • Mercury (公开)

    您将使用 Mercury.sharedInstance 来发布 Notifications。您可以告诉 Mercury 哪个时候 等待(wait) 收集通知,何时 执行(go) 发布通知。

  • 通知 (公开,可扩展)

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

  • 通知视图 (公开,可扩展)

    NotificationView 是一个用于显示 Notifications 的 UIView。Mercury 允许您子类化和显示符合您应用程序风格的自己的 NotificationViews。

  • 公告视图 (受保护)

    BulletinView 是一个显示 1 个或多个 NotificationViews 的 UIView。Mercury 不允许您使用自己的 BulletinView 进行子类化。

如何使用

两步简单易行!

  1. 创建一个或多个 Notifications
  2. mercury.postNotifications([...])

示例代码

创建 Notifications

// uses Mercury singleton
let mercury = Mercury.sharedInstance

// 'Upload Complete!' success Notification
let successNotification = MercuryNotification()
successNotification.text = "Upload complete!"
successNotification.image = UIImage(named: "success_icon")
successNotification.color = .greenColor()
successNotification.userInfo = ["key" : "Can use dictionaries"]

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

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

发布 Notifications

如果当你已经显示Bulletin时发出 Notifications,它会在当前Bulletin关闭后显示所有新的 Notifications
mercury.postNotification(successNotification)
mercury.postNotification(failureNotification)
你可以告诉 Mercury 排队并在显示它们之前收集一组 Notifications
// we could do use wait(), which tells Mercury to collect notifications without showing them yet
mercury.wait()

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

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

继承 MercuryNotificationView

继承 MercuryNotificationView 非常简单,并赋予您根据需要进行视图设计,使其尽可能简单或复杂。

class CustomNotificationView: MercuryNotificationView {
  override var notification: MercuryNotification? {
    didSet {
      // update your view
    }
  }
}

接下来,您需要实现如下 MercuryDelegate

mercury.delegate = self

以及代理方法

func mercuryNotificationViewForNotification(#mercury: Mercury, notification: MercuryNotification) -> MercuryNotificationView? {
        if notification.tag == kCustomNotificationTag {
            return CustomNotificationView()
        }
        return nil
    }

您可以为 MercuryNotifications 添加标签,以帮助确定使用哪个自定义 MercuryNotificationView,或者总是返回您的自定义通知视图。这取决于您!