目前我的 swift
中有一个字符串数组,我在 UILabel
上循环显示它们:
let greetings = ["Test1", "Test2", "Test3", "Test4"]
override func viewDidLoad(){
super.viewDidLoad()
Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(TutorialEntryPoint.update), userInfo: nil, repeats: true)
}
var i = 0
func update() {
if(i==4){
i=0
}
myLabel.text = greetings[i]
i += 1
}
这行得通,但是每个文本突然消失和显示 - 有没有办法修改它以便每个文本顺利消失/显示?我考虑过使用 animateWithDuration
并修改 alpha
,但我不确定如何正确地进行。
最佳答案
您可以像这样在标签上设置动画
UIView.transition(with: label,
duration: 0.25,
options: [.transitionCrossDissolve],
animations: {
label.text = "Your Text"
}, completion: nil)
关于ios - 如何在 Swift 中淡入和淡出同一个 UILabel 上的不同文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40499520/