Ios 在UITableViewCell中逐行显示字符串的动画
我有一个UITableView,对于每个单元格,我需要显示一个包含换行符的字符串。我想像打字机一样显示文本,但要逐行显示:Ios 在UITableViewCell中逐行显示字符串的动画,ios,swift,uitableview,grand-central-dispatch,Ios,Swift,Uitableview,Grand Central Dispatch,我有一个UITableView,对于每个单元格,我需要显示一个包含换行符的字符串。我想像打字机一样显示文本,但要逐行显示: 显示一行 等几秒钟 显示第二行 等几秒钟 字符串示例: let myString="ipsum dolor sit amet \n consectetur adipiscing elit\n Vestibulum interdum felis arcu\n quis iaculis dolor malesuada ut" 代码: 使用此代码,所有文本一次显示在一个块中
- 显示一行李>
- 等几秒钟
- 显示第二行
- 等几秒钟
let myString="ipsum dolor sit amet \n consectetur adipiscing elit\n Vestibulum interdum felis arcu\n quis iaculis dolor malesuada ut"
代码:
使用此代码,所有文本一次显示在一个块中。
我想我应该在每行中断之间添加一个延迟时间,所以我尝试在许多地方添加一个asyncAfter,但没有成功
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
}
我分享我的解决方案
func writingTextAnimation(dialogueLabelCell:UILabel, text:String, completion:@escaping ()->()) {
let delimiter = "\n"
var lines = text.components(separatedBy: delimiter)
var index = 0
animationTimer = Timer.scheduledTimer(withTimeInterval: TYPING_TIME_INTERVAL, repeats: true, block: { (timer: Timer) in
if index < lines.count {
dialogueLabelCell.text = dialogueLabelCell.text! + lines[index] + delimiter
self.tableView.beginUpdates()
self.tableView.endUpdates()
index = index + 1
} else {
self.animationTimer?.invalidate()
self.animationTimer = nil
completion()
}
})
}
func writingTextAnimation(对话框UELabelCell:UILabel,文本:String,完成:@escaping()->()){
let delimiter=“\n”
变量行=text.components(分隔符:分隔符)
var指数=0
animationTimer=Timer.scheduledTimer(withTimeInterval:TYPING_TIME_INTERVAL,repeats:true,block:{(Timer:Timer)in
如果索引
也许此库可以作为您正在寻找的案例的参考。谢谢,它很有用!是我错了还是这个dialogueLabelCell.text=dialogueLabelCell.text!+行[索引]
应该是这个dialogueLabelCell.text=dialogueLabelCell.text!+行[索引]+分隔符
?
func writingTextAnimation(dialogueLabelCell:UILabel, text:String, completion:@escaping ()->()) {
let delimiter = "\n"
var lines = text.components(separatedBy: delimiter)
var index = 0
animationTimer = Timer.scheduledTimer(withTimeInterval: TYPING_TIME_INTERVAL, repeats: true, block: { (timer: Timer) in
if index < lines.count {
dialogueLabelCell.text = dialogueLabelCell.text! + lines[index] + delimiter
self.tableView.beginUpdates()
self.tableView.endUpdates()
index = index + 1
} else {
self.animationTimer?.invalidate()
self.animationTimer = nil
completion()
}
})
}