gpt4 book ai didi

ios - 检索下一个文本时,字符串逐个字母的动画会混淆

转载 作者:行者123 更新时间:2023-11-29 05:50:54 25 4
gpt4 key购买 nike

我有一个弹出窗口,应该用逐个字母的动画显示用户说明。问题是每当用户单击“下一步”时,字母就会与之前的文本混淆。

动画代码:

extension UILabel {
func animate(newText: String, characterDelay: TimeInterval) {
DispatchQueue.main.async {
self.text = ""

for (index, character) in newText.enumerated() {
DispatchQueue.main.asyncAfter(deadline: .now() + characterDelay * Double(index)) {
self.text?.append(character)
}
}
}
}

按钮操作/调用方法(如您所见,每次用户单击“下一步”时我都尝试清空变量,但没有成功)

@IBAction func nextbtt(_ sender: Any) {
var instructions = ["text"]
counter = counter + 1
var w1 = " لكن الوصول إليه يتطلب مواجهة وحل تحديات مختلفة"
var w2 = "هل بإمكانك مساعدتي في الحصول على الكنز؟"

let userId = UserDefaults.standard.object(forKey: "userId") as? String
ref = Database.database().reference()
let userLang = ref.child("users").child(userId!).child("lang")
userLang.observeSingleEvent(of: .value, with: { (snapshot) in
let lang = snapshot.value as? Int
if(lang==1){
////////////// if user's langague is English
w1 = "But finding it requires confronting and solving different challenges"
w2 = " Could you help me in getting the treasure" }

instructions.append(w1)
instructions.append(w2)

if(self.intrCounter < 3){
self.mytext.text = ""

var new = instructions[self.intrCounter]

self.mytext.text = new

self.mytext.animate(newText: new ?? "May the source be with you", characterDelay: 0.1)
self.intrCounter = self.intrCounter + 1

if(self.intrCounter == 3){
if(lang==1){
(sender as AnyObject).setBackgroundImage(UIImage(named: "engready"), for: UIControl.State.normal)
}
else {
(sender as AnyObject).setBackgroundImage(UIImage(named: "ready"), for: UIControl.State.normal)}
}
}
else{
}
})

if ( counter == 4){
status[0] = true
popUp.removeFromSuperview()
}
}

截图:

一:text is showing to the user, user clicks "next" at the middle of the animation

两个:when user clicks next before text 1 is complete代码:

最佳答案

出现此问题的原因是您正在为每个字符调用异步 block ,并且每个字符可能需要不同的时间。只是为了测试这个尝试这个更改,如果它有助于相应地更改代码:

var someCounter = 1

extension UILabel {
func animate(newText: String, characterDelay: TimeInterval) {
DispatchQueue.main.async {
self.text = ""

for (index, character) in newText.enumerated() {
someCounter += 1
DispatchQueue.main.asyncAfter(deadline: .now() +
someCounter + characterDelay * Double(index)) {
self.text?.append(character)
}
}
}
}

请告诉我这是否确实是问题所在,如果是,我将上传更优化的代码。

关于ios - 检索下一个文本时,字符串逐个字母的动画会混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55677943/

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