A UILabel
子类,它添加了打字动画效果 —— 就像有一个
这个 pod 是受到以下帖子的启发:这里。
通过 CocoaPods 安装
要使用 CocoaPods 将 GhostTypewriter
集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
pod 'GhostTypewriter'
然后,运行以下命令
$ pod install
要求 CocoaPods 1.1.1+ 版本来构建
GhostTypewriter
。
用法
TypewriterLabel
是 UILabel
的子类,动画(魔法)在这里发生。它通过利用标签上的 attributedText
属性,并改变文本内容的属性来实现逐渐显示文本,类似机械打字机上的动画。
将 TypewriterLabel
实例作为子视图添加时,将隐藏其内容。
开始
开始动画将逐个字符显示标签的内容。
每个字符显示的速度可以通过设置
typingTimeInterval
属性来控制。
启动动画有两种方式:带完成闭包和不带完成闭包。
带完成闭包
import GhostTypewriter
@IBAction func startAnimationButtonPressed(_ sender: Any) {
titleLabel.startTypewritingAnimation {
//Implement your completion closure body here...
}
}
不带完成闭包
import GhostTypewriter
@IBAction func startAnimationButtonPressed(_ sender: Any) {
titleLabel.startTypewritingAnimation()
}
停止
停止动画会导致已经显示的字符保持原样,不会显示新字符。
import GhostTypewriter
@IBAction func stopAnimationButtonPressed(_ sender: Any) {
titleLabel.stopTypewritingAnimation()
}
重置
重置动画会导致所有字符被隐藏。
import GhostTypewriter
@IBAction func resetAnimationButtonPressed(_ sender: Any) {
titleLabel.resetTypewritingAnimation()
}
需要注意的是,重置 TypewriterLabel
实例不会使动画重新开始,您需要调用 restartAnimationButtonPressed()
方法。
重新开始
重新开始动画会导致所有字符被隐藏,并从开始再次开始动画。
启动动画有两种方式:带完成闭包和不带完成闭包。
不带完成闭包
import GhostTypewriter
@IBAction func restartAnimationButtonPressed(_ sender: Any) {
titleLabel.restartTypewritingAnimation()
}
带完成闭包
import GhostTypewriter
@IBAction func restartAnimationButtonPressed(_ sender: Any) {
titleLabel.restartTypewritingAnimation {
//Implement your completion closure body here...
}
}
完成
完成动画会导致所有字符立即显示。
import GhostTypewriter
@IBAction func completeAnimationButtonPressed(_ sender: Any) {
titleLabel.completeTypewritingAnimation()
}
动画选项
默认情况下,TypewriterLabel
在从索引 0
到 n-1
显示每个字符之前会隐藏其内容,但可以更改此行为。
要隐藏每个字符而不是显示,将 animationStyle
属性设置为 .hide
import GhostTypewriter
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.animationStyle = .hide
}
animationStyle
默认为 .reveal
。
要从索引 n-1
到 0
返回,将 animationDirection
设置为 .backward
import GhostTypewriter
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.animationDirection = .backward
}
animationDirection
默认为 .forward
。
调整动画时机
TypewriterLabel
实例中的每个字符都是通过typingTimeInterval
属性设置的节奏进行显示。
typingTimeInterval
的默认值是0.1秒。
import GhostTypewriter
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.typingTimeInterval = 0.3
}
需要注意的是,在动画已经开始后设置或更改typingTimeInterval
,不会对该动画的时机产生影响。
故事板
由于TypewriterLabel
位于一个pod中,当与其一起使用故事板时,您需要将Module
字段设置为GhostTypewriter
。
从v1迁移到v2
GhostTypewriter
的2.0.0版本包含破坏性更改。
cancelTypewritingAnimation()
现在使用resetTypewritingAnimation()
。cancelTypewritingAnimation(clearText: true)
现在使用resetTypewritingAnimation()
。cancelTypewritingAnimation(clearText: false)
现在使用stopTypewritingAnimation()
。
示例
GhostTypewriter
附带一个示例项目,提供比上述内容更详细的说明。
遇到问题了吗?
如果您遇到特定于GhostTypewriterLabel的问题、有功能请求或想分享评论,请在这里新建问题。
我们欢迎并高度赞赏合并请求!请尽量与现有的代码风格保持一致。如果您正在考虑对项目进行重大更改或添加,请提前通过创建新的问题进行沟通。这可以让每个人都能跟上即将到来的更改,确保更改与项目的设计理念一致,并避免重复工作。