简体   繁体   English

UIView动画链接无法正常工作

[英]UIView Animation Chaining Doesn't Work Properly

I am trying to animate a simple Loading label text to show 3 dots after it, with each dot having a second of delay. 我正在尝试制作一个简单的“ 加载”标签文本的动画,以在其后显示3个点,每个点都有一秒钟的延迟。

Here is what i tried: 这是我尝试过的:

func animateLoading() {

    UIView.animate(withDuration: 1, animations: {self.yukleniyorLabel.text = "Yükleniyor."}, completion: { _ in
         UIView.animate(withDuration: 1, animations: {self.yukleniyorLabel.text = "Yükleniyor.."}, completion: { _ in
           UIView.animate(withDuration: 1, animations: {self.yukleniyorLabel.text = "Yükleniyor..."})
        })
    })
}

But what i got is the all 3 dots appear in 1 second alltogether. 但是我得到的是所有3个点一起出现在1秒内。 Not in order. 顺序不对。 See here: https://streamable.com/yiz6s 看到这里: https : //streamable.com/yiz6s

What am i doing wrong with the chaining? 链接发生了什么问题? Thanks in advance. 提前致谢。

UIView animate is only for animatable view properties such as frame and background color. UIView animate仅适用于可动画显示的视图属性,例如框架和背景色。 self.yukleniyorLabel.text is not an animatable property. self.yukleniyorLabel.text不是可设置动画的属性。 So you get no animation. 所以你没有动画。

Just use a Timer or delayed performance to change the text at time intervals. 只需使用计时器或延迟的性能即可按时间间隔更改文本。

You can use the scheduled Timer for showing text with three dots on the label with animation: -> 您可以使用预定的计时器在带有动画的标签上显示带有三个点的文本:->

var i = 0
var timer : Timer?

loaderLabel.text = "Loading"
timer =  Timer.scheduledTimer(timeInterval: 0.2, target: self, selector:#selector(ViewController.setText), userInfo: nil, repeats: true)


@objc func setText() {
    loaderLabel.text = loaderLabel.text! + "."
    i += 1
    if i >= 3 {
        timer?.invalidate()
    }
}

output with animation: -> Loading... 动画输出:->正在加载...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM