一个 UILabel
子类,添加打字机动画效果 - 就像一个 👻 直接在你的用户设备上打字一样!
GhostTypewriter
的灵感来自 这里 的帖子。
要使用 CocoaPods 将 GhostTypewriter
集成到你的 Xcode 项目中,请在你的 Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
pod 'GhostTypewriter'
然后,运行以下命令
$ pod install
需要 CocoaPods 1.1.1+ 才能构建
GhostTypewriter
。
Swift Package Manager 是 Xcode 内置的依赖管理器。
如果你使用的是 Xcode 11 或更高版本,请转到 File -> Add Packages...
并输入包仓库 URL https://github.com/wibosco/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
实例不会导致动画重新开始,而是需要调用 restartTypewritingAnimation()
。
重新开始动画会导致所有字符都被隐藏,并且动画从头开始。
有两种启动动画的方法:带和不带完成闭包。
不带完成闭包
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
在动画时显示内容,但是可以通过将 animationStyle
属性设置为 .hide
来更改为隐藏内容。
import GhostTypewriter
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.animationStyle = .hide
}
animationStyle
默认设置为.reveal
。
默认情况下,TypewriterLabel
从字符索引 0
到 n-1
播放动画,但是可以通过将 animationDirection
设置为 .backward
来更改为从字符索引 n-1
到 0
。
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
。
GhostTypewriter
的版本 2.0.0
包含重大更改。
cancelTypewritingAnimation()
现在使用 resetTypewritingAnimation()
。cancelTypewritingAnimation(clearText: true)
现在使用 resetTypewritingAnimation()
。cancelTypewritingAnimation(clearText: false)
现在使用 stopTypewritingAnimation()
。
GhostTypewriter
附带一个 示例项目,以提供比上面列出的更多详细信息。
如果你遇到 GhostTypewriterLabel 特有的问题,有功能要求或想分享评论,请在此处打开一个新 Issue。
鼓励并非常感谢 Pull 请求! 请尽量与现有的代码风格保持一致。 如果你正在考虑对项目进行重大更改或添加,请提前通过打开新 Issue 进行沟通。 这可以让每个人都参与到即将到来的更改中,确保更改与项目的设计理念保持一致,并避免重复工作。