高度可配置的 iOS UIView,用于显示通知,且不会阻塞 UI。
SwiftyNotifications 版本 | 最低 iOS 目标 | Swift 版本 |
---|---|---|
0.7.1 | 11.0 | 5.x |
0.5.3 | 9.0 | 4.2 |
0.5.2 | 9.0 | 4.1 |
0.5.1 | 8.0 | 4.0 |
0.4 | 8.0 | 3.x |
CocoaPods 是 Cocoa 项目的依赖管理工具。 您可以使用以下命令安装它
$ gem install cocoapods
要使用 CocoaPods 将 SwiftyNotifications 集成到您的 Xcode 项目中,请在您的 Podfile 中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '11.0'
use_frameworks!
target '<Your Target Name>' do
pod 'SwiftyNotifications', '~>0.7.1'
end
然后,运行以下命令
$ pod install
Carthage 是一个去中心化的依赖管理工具,它可以构建您的依赖项并为您提供二进制框架。
您可以使用 Homebrew 通过以下命令安装 Carthage
brew update
brew install carthage
要使用 Carthage 将 SwiftyNotifications 集成到您的 Xcode 项目中,请在您的 Cartfile 中指定它
github "abdullahselek/SwiftyNotifications" ~> 0.7.1
修改您的 Package.swift 文件以包含以下依赖项
.package(url: "https://github.com/abdullahselek/SwiftyNotifications.git", from: "0.7.1")
XCFrameworks 需要 Xcode 11 或更高版本,并且集成方式与 .framework 格式的集成非常相似。 请使用脚本 scripts/build-framework.sh 生成二进制 SwiftyNotifications.xcframework 归档文件,您可以在 Xcode 中将其用作依赖项。
SwiftyNotifications.xcframework 是一个发布(优化)二进制文件,可提供最佳的可用 Swift 代码性能。
import SwiftyNotifications
然后初始化通知并将其添加到您的视图
let notification = SwiftyNotifications.withStyle(style: .info,
title: "Swifty Notifications",
subtitle: "Highly configurable iOS UIView for presenting notifications that doesn't block the UI",
direction: .bottom)
view.addSubview(notification)
您可以随时在视图中自定义此通知
notification.customize(style: .warning)
并更新文本
notification.setTitle(title: "New title", subtitle: "New subtitle")
要显示通知
notification.show()
要关闭
notification.dismiss()
创建自定义通知
let customNotification = SwiftyNotifications.withStyle(style: .custom,
title: "Custom",
subtitle: "Custom notification with custom image and colors",
direction: .top)
customNotification.leftAccessoryView.image = UIImage(named: "apple_logo")!
customNotification.setCustomColors(backgroundColor: UIColor.cyan, textColor: UIColor.white)
view.addSubview(customNotification)
创建通知的其他可用函数
使用自动关闭的时间间隔选项
let notification = SwiftyNotifications.withStyle(style: .warning,
title: "Title",
subtitle: "Subtitle",
dismissDelay: 3.0,
direction: .top)
带有触摸处理程序
let notification = SwiftyNotifications.withStyle(style: .error,
title: "Title",
subtitle: "Subtitle",
dismissDelay: 5.0,
direction: .bottom) {
}
添加了一个新类,仅用于显示消息
let swiftyNotificationsMessage = SwiftyNotificationsMessage.withBackgroundColor(color: UIColor.darkGray,
message: "Notification with just text",
direction: .top)
view.addSubview(swiftyNotificationsMessage)
显示消息通知
swiftyNotificationsMessage.show()
关闭消息通知
swiftyNotificationsMessage.dismiss()
可选的代理,提供有关显示和关闭通知屏幕的信息
func willShowNotification(notification: SwiftyNotifications)
func didShowNotification(notification: SwiftyNotifications)
func willDismissNotification(notification: SwiftyNotifications)
func didDismissNotification(notification: SwiftyNotifications)
添加触摸处理程序以捕获通知上的点击手势
notification.addTouchHandler {
}
添加一个滑动手势识别器,以通过滑动方向关闭通知
notification.addSwipeGestureRecognizer(direction: .down)
可能的滑动方向