NotificationBannerSwift 3.2.1

NotificationBannerSwift 3.2.1

测试已测试
Lang语言 SwiftSwift
许可证 MIT
Released最后发布2023年4月
SPM支持SPM

Dalton Hinterscher维护。



 
依赖
SnapKit~> 5.6.0
MarqueeLabel~> 4.3.0
 

  • 作者
  • Daltron

Notification Banner

GitHub contributors License PayPal

编写于Swift 5

NotificationBanner是一个极富可定制性和轻量级的库,它使得在iOS中显示应用通知横幅和下拉警报变得极为简单。

基本横幅 带侧边栏的横幅 状态栏横幅
Basic Banners Banners with Side Views Status Bar Banners
增长横幅 悬浮横幅 堆叠横幅
Growing Banners Floating Banners Floating Banners

功能

  • 高度可定制
  • 支持 NSAttributedString
  • 支持iPhone, iPhoneX, & iPad
  • 支持方向变化
  • 支持自定义 UIView
  • 支持自定义颜色
  • 支持长标题/副标题
    • NotificationBanner 使用水平滚动标签
    • GrowingNotificationBanner 随需增长高度
  • 支持从顶部或底部显示
  • 支持振动反馈
  • 内置横幅队列
  • 允许同时显示多个横幅,可在横幅队列中配置
  • 支持无障碍访问
  • 支持动态岛

需求

  • iOS 10.0+
  • Xcode 10.0+

安装

CocoaPods

NotificationBanner 可通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中

Swift 5 + Xcode 11 + iOS 13 支持

pod 'NotificationBannerSwift', '~> 3.0.0'

Swift 5 + Xcode 10.x

pod 'NotificationBannerSwift', '2.5.0'

Swift 4.2

pod 'NotificationBannerSwift', '2.0.1'

Swift 4.0

pod 'NotificationBannerSwift', '1.6.3'
pod 'MarqueeLabel/Swift', '3.1.6'

然后在项目中使用NotificationBanner的每个文件顶部添加import NotificationBannerSwift

Carthage

要使用通过Carthage的NotificationBanner,只需将此行添加到您的Cartfile

Swift 5

github "Daltron/NotificationBanner" "master"

然后,在项目中添加NotificationBanner.framework以及依赖的SnapKit.frameworkMarqueeLabel.framework

Swift Package Manager

您也可以将此库作为swift包管理器添加。

  1. 转到文件 > 添加包
  2. 出现添加包对话框,默认包含Apple包。
  3. 在右上角,将 https://github.com/Daltron/NotificationBanner 粘贴到搜索栏
  4. 按 Return 键开始搜索
  5. 点击添加包。

使用说明

使用 NotificationBanner 创建下拉警告非常简单。要创建一个普通的横幅(带有滚动标签)并显示它,只需

let banner = NotificationBanner(title: title, subtitle: subtitle, style: .success)
banner.show()

如果您想要创建一个随着需要增长高度的横幅并相应地显示它,只需使用 GrowingNotificationBanner 而不是 NotificationBanner

let banner = GrowingNotificationBanner(title: title, subtitle: subtitle, style: .success)
banner.show()

要创建状态栏警告,只需

let banner = StatusBarNotificationBanner(title: title, style: .success)
banner.show()

默认情况下,每个横幅都会显示在主应用程序窗口上。如果您想要在导航栏下方显示横幅,只需在导航系统内的视图控制器中显示即可

banner.show(on: viewController)

默认情况下,每个横幅都会从顶部显示。如果您想从底部显示,只需

banner.show(bannerPosition: .bottom)

上面定义的每个显示属性都可以混合匹配,彼此完美协同工作。

默认情况下,每个横幅将在 5 秒后自动消失。要程序性地关闭,只需

banner.dismiss()

要无限期显示横幅直到手动关闭,只需

banner.autoDismiss = false

NotificationBanner 提供了五个预设样式供您选择

public enum BannerStyle {
    case danger
    case info
    case customView
    case success
    case warning
}

您可以通过实现 BannerColorsProtocol 来覆盖 NotificationBanner 对任何样式预定义的颜色

public protocol BannerColorsProtocol {
    func color(for style: BannerStyle) -> UIColor
}

创建自定义横幅颜色类就像这樣容易

class CustomBannerColors: BannerColorsProtocol {

    internal func color(for style: BannerStyle) -> UIColor {
        switch style {
            case .danger:	// Your custom .danger color
            case .info:		// Your custom .info color
            case .customView:	// Your custom .customView color
            case .success:	// Your custom .success color
            case .warning:	// Your custom .warning color
        }
    }

}

然后将该类传给任何创建的通知横幅

let banner = NotificationBanner(title: title, style: .success, colors: CustomBannerColors())
banner.show()

默认情况下,如果没有在初始化方法中提供样式,横幅将应用 .info 样式。您可以通过简单地设置 backgroundColor 来在任何时候设置横幅的背景颜色。

带侧视图的横幅

通知横幅可以有一个左侧视图,一个右侧视图,或者两者都有

// Success Style Notification with Left View
let leftView = UIImageView(image: #imageLiteral(resourceName: "success"))
let banner = NotificationBanner(title: title, subtitle: subtitle, leftView: leftView, style: .success)
banner.show()

 // Danger Style Notification with Right View
let rightView = UIImageView(image: #imageLiteral(resourceName: "danger"))
let banner = NotificationBanner(title: title, subtitle: subtitle, rightView: rightView, style: .danger)
banner.show()    

// Info Style Notification with Left and Right Views
let leftView = UIImageView(image: #imageLiteral(resourceName: "info"))
let rightView = UIImageView(image: #imageLiteral(resourceName: "right_chevron"))
let banner = NotificationBanner(title: title, subtitle: subtitle, leftView: leftView, rightView: rightView, style: .info)
banner.show()

Banners with Side Views

每个侧视图都会自动调整大小以完全适应

带自定义视图的横幅

通知横幅也可以使用自定义视图初始化

let banner = NotificationBanner(customView: NorthCarolinaBannerView())
banner.show()

Custom Banner

处理用户交互

默认情况下,当用户点击或向上滑动横幅时,它将被忽略。如果想要检测用户是否点击或向上滑动横幅,只需简单地进行以下操作:

banner.onTap = {
	// Do something regarding the banner
}
banner.onSwipeUp = {
	// Do something regarding the banner
}

横幅事件

可以通过作为其代理注册以选择加入通知横幅的事件

banner.delegate = self

然后只需确保实现以下方法

func notificationBannerWillAppear(_ banner: BaseNotificationBanner)
func notificationBannerDidAppear(_ banner: BaseNotificationBanner)
func notificationBannerWillDisappear(_ banner: BaseNotificationBanner)
func notificationBannerDidDisappear(_ banner: BaseNotificationBanner)

触觉反馈支持

默认情况下,当横幅显示时,支持的设备将产生触觉反馈。触觉反馈的类型如下:

public enum BannerHaptic {
	case light
   	case medium
	case heavy
  	case none
}

为了在显示横幅时更改触觉反馈的类型,只需以下操作:

banner.haptic = .heavy

横幅队列

默认情况下,每个通知横幅都会放置在一个自动管理的《NotificationBannerQueue》单例队列中。这允许多个横幅同时显示,而不会相互遮挡。如果你在导航堆栈中有多个控制器需要由单独的队列管理(如标签栏控制器),只需创建一个《NotificationBannerQueue》实例并将其传递给show函数

banner.show(queue: customQueue)

默认情况下,每个通知横幅都会放在队列的末尾。如果你想将横幅置于队列前端并立即显示,不受队列中横幅数量的影响,只需在show()方法中指定即可

banner.show(queuePosition: .front)

将横幅添加到队列前端将暂时挂起当前显示的横幅(如果有),并在前方横幅消失后恢复

要获取当前队列中的横幅数量,只需

let numberOfBanners = NotificationBannerQueue.default.numberOfBanners

一切都是自动管理的!

横幅队列和同时显示横幅(堆叠)

你还可以创建一个队列来同时显示多个横幅,并控制同时显示的最大横幅数量。你可以显示比队列设置允许的更多横幅 - 超过此值的横幅将稍后显示,在屏幕上已经显示的某些横幅关闭后。以下示例中,我们创建了最多允许同时显示3个横幅的队列

let bannerQueueToDisplaySeveralBanners = NotificationBannerQueue(maxBannersOnScreenSimultaneously: 3)

创建五个不同的横幅

let banner1 = FloatingNotificationBanner(
	title: "Success Notification - 1",
	subtitle: "First Notification from 5 in current queue with 3 banners allowed simultaneously",
	style: .success
)
banner1.delegate = self

let banner2 = FloatingNotificationBanner(
	title: "Danger Notification - 2",
	subtitle: "Second Notification from 5 in current queue with 3 banners allowed simultaneously",
	style: .danger
)
banner2.delegate = self

let banner3 = FloatingNotificationBanner(
	title: "Info Notification - 3",
	subtitle: "Third Notification from 5 in current queue with 3 banners allowed simultaneously",
	style: .info
)
banner3.delegate = self

let banner4 = FloatingNotificationBanner(
	title: "Success Notification - 4",
	subtitle: "Fourth Notification from 5 in current queue with 3 banners allowed simultaneously",
	style: .success
)
banner4.delegate = self

let banner5 = FloatingNotificationBanner(
	title: "Info Notification - 5",
	subtitle: "Fifth Notification from 5 in current queue with 3 banners allowed simultaneously",
	style: .info
)
banner5.delegate = self

并一次性显示所有五个横幅

showBanners(
	[banner1, banner2, banner3, banner4, banner5],
	in: bannerQueue5AllowedMixed
)

使用此辅助方法

func showBanners(
	_ banners: [FloatingNotificationBanner],
	in notificationBannerQueue: NotificationBannerQueue
) {
    banners.forEach { banner in
      	banner.show(
	      	bannerPosition: selectedBannerPosition(),
		 	queue: notificationBannerQueue,
		 	cornerRadius: 8,
			shadowColor: UIColor(red: 0.431, green: 0.459, blue: 0.494, alpha: 1),
		  	shadowBlurRadius: 16,
			shadowEdgeInsets: UIEdgeInsets(top: 8, left: 8, bottom: 0, right: 8)
	   )
    }
}

它将一次性显示前三个横幅,然后在一段时间后(或由用户触摸)隐藏,并显示4号和5号横幅。所有这一切都以花哨的动画效果完成。

功能需求

很想知道您认为 NotificationBanner 缺少什么功能。请打开一个问题,我会给它添加 功能请求 标签,如果这个请求符合库的最佳利益,我会尽我所能来满足它。😄

使用 NotificationBanner 的应用程序

Q - Talk About Music VH Dispatch Stikkr CardCast Happy Scale Wanderings Modern Magic 8 Ball Envision: Habits Tracker TSUM RIS LukaPizza

请自由添加您的应用程序!

作者

Dalton Hinterscher, [email protected]

许可证

NotificationBanner 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。