测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可 | MIT |
发布最新发布 | 2016年8月 |
SPM支持SPM | ✗ |
由Shannon Wu维护。
您还可以观看这个简短的Youtube视频,了解它通常可以做什么。
您还可以观看这个简短的Youtube视频,了解更多关于其实现细节的信息。
StatusBarNotificationCenter
是一个库,可以在您的应用程序中使用,以显示自定义状态栏通知。
注意:在我们的软件开发过程中,我们想要找到一个可以显示状态栏通知的库,该项目从流行的CWStatusBarNotification
库中学到了很多思想,但代码实现(在我的意见中)更加简洁,并且完全用Swift 2.0编写,而且更易于扩展,还提供了更多自定义选项,并支持iOS9+的多任务处理和分屏视图。如果您想要找到一个自定义状态栏通知库,您可以查看它。
现在,您可以通过设置StatusBarNotificationCenter配置中的userInteractionEnabled标志来让用户在通知显示时与应用程序交互,您可以通过查看最新的提交来了解添加此功能是多么简单。
这个库仅仅是一个包含窗口和视图控制器的中枢类,并且中心维护一个通知队列,我想我们的主要目标是建立一个稳定且可维护的架构,我想添加尽可能多的功能,但我真的很喜欢简单的架构,所以必须有某种平衡,我只是几个月的程序员的程序员,所以可能会有一些不合适的地方,如果您有任何建议,请用我的邮箱联系我,您很容易将自定义视图添加到这个库中,如果您有伟大之处要分享,请打开一个问题或提交一个pull请求,感谢您的支持。
要运行示例项目,首先克隆仓库,然后在 Example 目录中运行 pod install
。
此示例最佳在横向模式下的 iPad air 或 iPad Pro 上运行,并且你可以测试分割视图。
由于演示代码使用的是 UIStackView 编写,因此需要 Xcode 7.0+ 和 iOS 9.0+ 来运行演示,但我想这个库可以用拥有系统版本 7.0+ 的设备使用,因为 API 非常基础,你可以稍加修改源代码,以配合你的最小部署目标。
在 iPhone 和 iPad 上运行
StatusBarNotificationCenter 通过 CocoaPods 提供。要安装它,只需在 Podfile 中添加以下行,因为这是用 Swift 2.0 编写的,你可能还需要在 Podfile 中插入 use_frameworks!
pod "StatusBarNotificationCenter"
首先,你需要导入 StatusBarNotificationCenter
框架
其次,你必须提供一个 NotificationCenterConfiguration
对象,默认实现如下
/**
* Customize the overall configuration information of the notification, most of the property's default value is OK for most circumstance, but you can customize it if you want
*/
public struct NotificationCenterConfiguration {
/// The window below the notification window, you must set this property, or the notification will not work correctly
var baseWindow: UIWindow
/// The style of the notification, default to status bar notification
public var style = StatusBarNotificationCenter.Style.StatusBar
/// The animation type of the notification, default to overlay
public var animationType = StatusBarNotificationCenter.AnimationType.Overlay
/// The animate in direction of the notification, default to top
public var animateInDirection = StatusBarNotificationCenter.AnimationDirection.Top
/// The animate out direction of the notification, default to top
public var animateOutDirection = StatusBarNotificationCenter.AnimationDirection.Top
/// Whether the user can tap on the notification to dismiss the notification, default to true
public var dismissible = true
/// The animate in time of the notification
public var animateInLength: NSTimeInterval = 0.25
/// The animate out time of the notification
public var animateOutLength: NSTimeInterval = 0.25
/// The height of the notification view, if you want to use a custom height, set the style of the notification to custom, or it will use the status bar and navigation bar height
public var height: CGFloat = 0
/// If the status bar is hidden, if it is hidden, the hight of the navigation style notification height is the height of the navigation bar, default to false
public var statusBarIsHidden: Bool = false
/// The height of the navigation bar, default to 44.0 points
public var navigationBarHeight: CGFloat = 44.0
/// Should allow the user to interact with the content outside the notification
public var userInteractionEnabled = true
/**
Initializer
- parameter baseWindow: the base window of the notification
- returns: a default NotificationCenterConfiguration instance
*/
public init(baseWindow: UIWindow) {
self.baseWindow = baseWindow
}
}
注意:当你想显示通知时,你必须提供此通知的基本窗口,这个属性主要用于捕捉你的应用程序下层的视图快照,普通窗口将是你的应用程序视图的窗口
view.window!
如果你想通过自定义视图显示通知,只需使用配置对象调用类方法即可
StatusBarNotificationCenter.showStatusBarNotificationWithView(view, forDuration: NSTimeInterval(durationSlider.value), withNotificationCenterConfiguration: notificationCenterConfiguration)
此方法将显示通知,并持续指定的时间,如果配置对象的 dismissible 属性为 true,用户可以通过点击状态栏来关闭通知,如果你想显示通知并手动关闭它,你可以调用以下方法而不是这个方法
func showStatusBarNotificationWithView(view: UIView, withNotificationCenterConfiguration notificationCenterConfiguration: NotificationCenterConfiguration, whenComplete completionHandler: Void -> Void)
你可以提供完成处理程序,当显示完成后将被调用,但你必须自己关闭它,如果你的配置对象的 dismissible 属性为 true,用户可以通过点击状态栏来关闭通知
如果你要以字符串值的形式显示通知,你还必须传递一个 NotificationLabelConfiguration
对象,此对象的默认实现如下
/**
* If you use the default label to show the notification, you should send a customized configuration struct, the dufault implementation is a non-scrollabel label, with one line to show the information
*/
public struct NotificationLabelConfiguration {
/// if the label should scroll the content, default to false
public var scrollabel = true
/// If you set the scrollable property to true, you can use this property to customize the scroll delay, default delay is 1 second
public var scrollDelay: NSTimeInterval = 1.0
/// If you set the scrollabel property to true, you can use this property to customize the scroll speed, default speed is 40 points per second
public var scrollSpeed: CGFloat = 40.0
/// Set the padding of the message label, default to 10.0 points
public var padding: CGFloat = 10.0
/// if the label should be multiline implementation, default to false
public var multiline = false
/// The background color of the notification view, default to black color
public var backgroundColor = UIColor.blackColor()
/// The text color of the notification view, default to white color
public var textColor = UIColor.whiteColor()
/// The font of the notification label, default to a system font of size 14.0, if you pass the attributed string, this property will be ignored
public var font = UIFont.systemFontOfSize(StatusBarNotificationCenter.defaultMessageLabelFontSize)
/// this property is not nil, the label will use the attributed string to show the message
public var attributedText: NSAttributedString? = nil
/**
Init a new default notification label configuration
- returns: a new default notification label configuration
*/
public init() {
}
}
配置非常明显,你可以调用以下方法来调用通知
StatusBarNotificationCenter.showStatusBarNotificationWithMessage(notificationTextField.text, forDuration: NSTimeInterval(durationSlider.value), withNotificationCenterConfiguration: notificationCenterConfiguration, andNotificationLabelConfiguration: notificationLabelConfiguration)
还有一个类似的方法,使用方法与自定义视图的通知类似
func showStatusBarNotificationWithMessage(message: String?, withNotificationCenterConfiguration notificationCenterConfiguration: NotificationCenterConfiguration, andNotificationLabelConfiguration notificationLabelConfiguration: NotificationLabelConfiguration, whenComplete completionHandler: Void -> Void)
此库尚未完全测试,如果您发现一些错误,请提交问题;特别是,此库还没有进行多线程测试,尽管我认为通过一些修改,它将正常工作,如果您发现,请不要犹豫告诉我或提交拉取请求。
还要感谢 CWStatusBarNotification
的辛勤工作,如果您决定使用状态栏通知,我认为您可以比较这两个库,最初,我想修改那个存储库,但我认为从头开始写一个库是一个更好的主意。
Shannon Wu,您可以通过 电子邮件、Twitter 或 微博 联系我
StatusBarNotificationCenter 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。