SwiftyTooltip
SwiftyToolTip 是一个用 Swift 编写的 tooltip 库,便于开发者向任何 UI 元素添加工具提示和描述
示例
要运行示例项目,请先克隆仓库,然后在 Example 目录中运行 pod install
安装
CocoaPods
CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它
$ gem install cocoapods
要使用 CocoaPods 将 SwiftyToolTip 集成到您的 Xcode 项目中,请在您的 Podfile 中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
target '<Your Target Name>' do
pod 'SwiftyToolTip'
end
然后,运行以下命令:
$ pod install
用法
SwiftyToolTip 可以在Swift中几乎所有UI元素上使用,以下是一些如何实现提示框的示例。别担心,你只需写一行代码!
UIView
给UIView,或其子类添加提示非常简单,看看下面的例子
view.addToolTip(description: "This is the text displayed in the tooltip")
view.addToolTip(description: "Look below in Tips and Tricks for a suggesting on structuring your tooltips", gesture: .longPress, isEnabled: true)
UIBarButtonItem
给UIBarButtonItem,或其子类添加提示同样简单,只需注意放置代码的位置。因为视图需要在屏幕上显示,所以我们必须在viewDidAppear()
函数中添加提示
override func viewDidAppear(_ animated: Bool) {
// Adding a tool tip to a UIBarButtonItem has to happen in the viewDidAppear
barButtonItem.addToolTip(description: "Add a new item to some list")
barButtonItem.addToolTip(description: Description.BarButtonItems.addButton, gesture: .longPress)
}
UITabBar
给UITabBar或其按钮添加提示需要仔细思考,但仍然可以在一行代码内完成。始终考虑将哪种手势添加到哪个提示,因为它们可能会覆盖已存在的手势。
给单个tabbar按钮添加提示
// Double tap does not work on tabbarButtons (Not sure why, yet!)
tabBarController?.tabBar.addToolTip(at: 0, description: Description.tabBar.storyboardViewController, gesture: .longPress, isEnabled: true)
tabBarController?.tabBar.addToolTip(at: 1, description: Description.tabBar.otherViewcontroller, gesture: .longPress, isEnabled: true)
使用UIView实现,给整个tabbar添加提示
tabBarController?.tabBar.addToolTip(description: Description.tabBar.tabbar, gesture: .doubleTap)
技巧、窍门和额外信息
-
当添加提示时,你可能无意中覆盖了一个现有的手势
-
当给具有具有提示的子视图的UIView添加提示时,确保为父视图使用不同的手势。
-
目前支持的手势是:双击和长按。
-
isEnabled 添加提示但不会显示。
-
使用嵌套结构来保持描述干净并集中在一处,你也可以使用其他结构,如json。这只是一个建议,我确信还有更好的存储这些数据的方法。示例
struct Description {
struct Label {
static let defaultLabel = "This is a Label, a Label displays text"
static let otherLabel = "This is another label, this label can also display text"
}
struct OtherUIElements {
static let segmentedControl = "A segmented control gives you 2 or more option to select from"
static let slider = "Adjust a slider to change a give range of numbers"
static let progressView = "The progress view shows you the progress of a certain task"
}
struct BarButtonItems {
static let backButton = "Go back to the previous view controller without doing anything"
static let cancelButton = "Go back to previous view controller and cancel all tasks or information given"
static let saveButton = "Saves the new data and continues to the next step"
static let addButton = "Opens a new screen where you can create a new thing for your app"
}
struct tabBar {
static let tabbar = "Here you can switch between different sections of the app"
static let storyboardViewController = "Here we display a tableview with some tooltips."
static let otherViewcontroller = "Here we display other stuff"
}
}
作者
BobDeKort, [邮箱保护,请翻转字母和数字]
许可协议
SwiftyToolTip 可在 MIT 许可协议下使用。有关更多信息,请参阅 LICENSE 文件。