gpt4 book ai didi

swift - 快速逐字显示文本

转载 作者:行者123 更新时间:2023-12-01 12:04:25 33 4
gpt4 key购买 nike

快速 5 iOS 13

试图一个字一个字地慢慢打印出一个句子。起草了这个有效的代码,但我担心如果我输入太长的句子,我会因为太多线程而导致手机崩溃。

    let textFeed = "Some text message made up of words"
var second = 1
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {

for word in textFeed.components(separatedBy: " ") {
var delay = DispatchTimeInterval.seconds(second)
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: {
self.infoText2.text = String(word)

print("word",word,second)
})
second = second + 1
}
})

关于以不同的方式做这件事,减少线程的任何想法?

最佳答案

一个简单的 rekusive func

printMessage(message: "I am a test")

func printMessage(message: String?, delay: TimeInterval = 1) {
let splits = message?.split(separator: " ", maxSplits: 1)
print(splits?.first)
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: {[weak self] in
guard splits?.count == 2 else {
return
}
self?.printMessage(message: String(splits?.last ?? ""))
})
}

关于swift - 快速逐字显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59195486/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com