一个简单的Swift框架,用于在Swift中显示警告和通知消息
//Fire a quick message with a theme!
let message = Message(message: "A simple message", theme: .success)
announce(message, on: .view(aView), withMode: .timed(5.0))
//Fire a message with a title and customized appearance
let appearance = MessageWithTitleAppearance(foregroundColor: .white, backgroundColor: .red)
let message = MessageWithTitle(title: "A title", message: "A message", appearance: appearance)
let token = announce(message, on: .viewController(aViewController), withMode: .indefinite)
token.dismiss()
是的,您可以!您只需要让那些视图符合Announcement
协议,并为他们提供一个可配置的Appearance
,它们应该能够正常运行。另外,确保视图在使用leading和trailing NSLayoutConstraints
时不会断裂,因为默认的Presenter
会使用它们。
struct MyCustomAnnouncementAppearance: Appearance {
let backgroundColor: UIColor
static func defaultAppearance() -> MyCustomAnnouncementAppearance {
return MyCustomAnnouncementAppearance(backgroundColor: .black)
}
}
final class MyCustomAnnouncement: UIView, Announcement {
let appearance: MyCustomAnnouncementAppearance
init(appearance: MyCustomAnnouncementAppearance? = nil) {
self.appearance = appearance ?? MyCustomAnnouncementAppearance.defaultAppearance()
}
}
是的,您可以!通过符合协议来编写自定义的Presenter
。您还可以忽略如果您的自定义视图不支持,则安装约束的默认行为。
struct MyCustomPresenter: Presenter {
let viewToDisplayReference: UIView
@discardableResult func present<T: Announcement>(announcement: T) -> DismissalToken where T : UIView {
// Install the view on the view to display, install constraints and create your own animation
return DismissalToken {
// Run the animations to dismiss the view, remove it from its context and etc
}
}
}
创建了自己的Presenter之后,您可以通过以下方式调用它:
let myCustomAnnouncement = MyCustomAnnouncement()
let myPresenter = MyCustomPresenter(viewToDisplayReference: UIView())
let token = announce(myCustomAnnouncement, withCustomPresenter: myPresenter)
token.dismiss()
只需在Theme
上创建一个扩展,返回您的外观,并为您的视图创建一个便利的初始化器。
extension Theme {
func appearanceForMyCustomView() -> MyCustomAppearance {
switch self {
case .success, .info, .warning, .danger:
return MyCustomAppearance.defaultAppearance()
}
}
}
final class MyCustomView: UIView, Announcement {
//default implementation goes here
convenience init(theme: Theme) {
self.init(appearance: theme.appearanceForMyCustomView())
}
}
要将Announce用作Swift Package Manager包,请将以下内容添加到Package.swift文件中。
import PackageDescription
let package = Package(
name: "HelloAnnounce",
dependencies: [
.Package(url: "https://github.com/corujautx/Announce.git", "1.0")
]
)
如果您不希望使用上述任何一个依赖管理器,则可以将Announce手动集成到项目中。
cd
命令进入您的顶级项目目录,如果您还没有将项目初始化为 git 仓库,请运行以下命令$ git init
$ git submodule add https://github.com/corujautx/Announce.git
$ git submodule update --init --recursive
打开新的 Announce
文件夹,并将 Announce.xcodeproj
拖放到您的应用程序的 Xcode 项目的项目导航器中。
它应该嵌套在您的应用程序的蓝色项目图标下方。它位于其他所有 Xcode 组的上方或下方并不重要。
在项目导航器中选择 Announce.xcodeproj
,并验证部署目标是否与您的应用程序目标一致。
接下来,在项目导航器中选择您的应用程序项目(蓝色项目图标),导航到目标配置窗口,并在侧边栏的 "Targets" 下选择应用程序目标。
在窗口顶部的标签栏中,打开 "General" 选项卡。
在 "Embedded Binaries" 部分下方单击 +
按钮。
您将看到两个不同的 Announce.xcodeproj
文件夹,每个文件夹中都有一个不同的 Announce.framework
版本,位于 "Products" 文件夹中。
您可以选择任意的 "Products" 文件夹。
选择 Announce.framework
。
这样就可以了!
Announce.framework
将自动添加为目标依赖项,链接框架和嵌入框架,并在复制文件构建阶段中完成,这是您在模拟器和设备上构建所需的所有内容。
+
按钮。Announce.framework
。Helio Costa 为我处理主题调色板提供帮助
Announce 以 MIT 许可证发布。有关详细信息,请参阅 LICENSE。