CTShowcase
CTShowcase是一个用于iOS的应用库,允许您使用静态或动态效果突出显示单个视图。
兼容性
CTShowcase可以同时从Objective-C和Swift代码中使用。
2.0+版本需要Swift 3和Xcode 8,2.2+版本需要Swift 4和Xcode 9,2.3+版本需要Swift 4.2和Xcode 10,2.4+版本需要Swift 5.0和Xcode 10.2
本文档展示了使用该库最新版本的示例。
安装
使用Carthage
要使用Carthage安装CTShowcase,请将以下行添加到Cartfile中
github "CTShowcase/CTShowcase" ~> 2.4
然后运行 carthage update
命令以构建框架,并将生成的 CTShowcase.framework
拖拽到您的 XCode 项目中。
使用 CocoaPods
要使用 CocoaPods 安装 CTShowcase,将以下行添加到您的 Podfile 中
pod "CTShowcase", "~> 2.4"
然后运行 pod install
命令,并使用创建的工作区从此处开始打开您的项目。
手动安装
只需将文件 CTShowcaseView.swift
和 CTRegionHighlighter.swift
添加到您的项目中即可。
运行示例项目
示例项目位于 Example 目录中。框架目标已经作为依赖添加,因此您可以直接运行它。
使用方法
CTShowcase 的使用非常简单。
创建一个 CTShowcaseView
的实例
let showcase = CTShowcaseView(title: "New Feature", message: "Here's a brand new button you can tap!", key: @"displayed") { () -> () in
print("This closure will be executed after the user dismisses the showcase")
}
为您布局中可用的视图设置展示
showcase.setup(for: newButton)
最后,显示展示
showcase.show()
就是这样!CTShowcaseView
将自动确定标题和消息的最佳显示位置。
您可以通过在展示上任何地方点击来关闭展示
CTShowcaseView
将使用 key
参数来判断展示是否曾经显示过,如果显示过则不会再次显示。如果您希望展示显示多次,请将 nil
作为旧值传递。同样,如果您不需要在闭包中执行任何操作,也请将其作为 nil
传递。
或者,您可以直接使用提供的便捷初始化器
let showcase = CTShowcaseView(title: "New Feature", message: "Here's a brand new button you can tap!")
您可以通过使用以下方法而不是 setup(for:)
方法来设置展示,可选地给出偏移量和边距值
showcase.setup(for: self.button, offset: CGPointZero, margin: 5)
偏移量将决定高亮相对于目标视图位置移动多少。边距决定了目标视图边框与高亮内部边框之间的间距。
配置
CTShowcaseView
公开了用于显示标题和消息的标签,因此您可以直接访问它们并设置其属性,如字体或颜色。
showcase.titleLabel.font = UIFont.boldSystemFont(ofSize: 15)
showcase.messageLabel.textColor = UIColor.yellow
CTShowcaseView
使用符合CTRegionHighlighter
协议的类的实例来绘制它的突出显示。
CTShowcase包含两个符合其协议的类,这样您就不需要创建自己的类即可开始使用它。这些类是:CTStaticGlowHighlighter
和CTDynamicGlowHighlighter
。每个类都可以绘制矩形或圆形突出显示,并具有其他属性,允许您自定义其外观。
###CTStaticGlowHighlighter
这是CTShowcaseView
默认使用的突出显示器。它绘制非动画突出显示。如果您不喜欢默认设置,可以在设置展示之前自定义其属性。
let showcase = CTShowcaseView(title: "New Feature", message: "Here's a brand new button you can tap!")
let highlighter = showcase.highlighter as! CTStaticGlowHighlighter
highlighter.highlightColor = UIColor.yellow
showcase.setup(for: self.button, offset: CGPointZero, margin: 5)
showcase.show()
结果将如下所示
###CTDynamicGlowHighlighter
这是静态突出显示器的动画版本。为了使用它,创建一个实例,并将其设置为CTShowcaseView
实例的突出显示器。
let showcase = CTShowcaseView(title: "New Feature", message: "Here's a brand new button you can tap!")
let highlighter = CTDynamicGlowHighlighter()
// Configure its parameters if you don't like the defaults
highlighter.highlightColor = UIColor.yellow
highlighter.animDuration = 0.5
highlighter.glowSize = 5
highlighter.maxOffset = 10
// Set it as the highlighter
showcase.highlighter = highlighter
showcase.setup(for: self.button)
showcase.show()
产生的效果将如下所示
如果您将其类型设置为圆形
highlighter.highlightType = .Circle
您将获得以下所示圆形突出显示
扩展CTShowcase
CTShowcase提供的类应该适用于大多数应用,但如果您想添加不同的突出显示效果,这也很容易做到。
只需创建一个新的符合CTRegionHighlighter
协议的类,并将其用作CTShowcaseView
实例的突出显示器。查看代码中的注释以了解协议中的draw(on:targetRect:)
和layer(for:targetRect:)
方法应该如何操作。