打字机效果文本animation
我试图用UILabel创build一个打字机animation效果,但是找不到任何答案。 UILabel是否是正确的对象? 我想要文本打印到屏幕上的string数组,如“login…打开文件夹…重新启动系统..”等我应该提到,我是新来的编码,我试过search文档和API参考,但没有运气。 如果值得一提的话,我现在正在学习SWIFT
更新: Xcode 7.0 GM•Swift 2.0
import UIKit class ViewController: UIViewController { @IBOutlet weak var myTypeWriter: UITextField! let myText = Array("Hello World !!!".characters) var myCounter = 0 var timer:NSTimer? func fireTimer(){ timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "typeLetter", userInfo: nil, repeats: true) } func typeLetter(){ if myCounter < myText.count { myTypeWriter.text = myTypeWriter.text! + String(myText[myCounter]) let randomInterval = Double((arc4random_uniform(8)+1))/20 timer?.invalidate() timer = NSTimer.scheduledTimerWithTimeInterval(randomInterval, target: self, selector: "typeLetter", userInfo: nil, repeats: false) } else { timer?.invalidate() } myCounter++ } override func viewDidLoad() { super.viewDidLoad() fireTimer() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
我写了一个名为CLTypingLabel的UILabel的子类,可以在GitHub上find。 这应该做你想要的。
安装CocoaPods之后,将下面的内容添加到您的Podfile中以使用它:
pod 'CLTypingLabel'
示例代码
将标签的类从UILabel更改为CLTypingLabel;
@IBOutlet weak var myTypeWriterLabel: CLTypingLabel!
在运行时,设置标签的文本会自动触发animation:
myTypeWriterLabel.text = "This is a demo of typing label animation..."
您可以自定义每个字符之间的时间间隔:
myTypeWriterLabel.charInterval = 0.08 //optional, default is 0.1
您可以随时暂停inputanimation:
myTypeWriterLabel.pauseTyping() //this will pause the typing animation myTypeWriterLabel.continueTyping() //this will continue paused typing animation
还有一个样本项目,来自cocoapods