ios - 如何在 Swift 中淡入和淡出同一个 UILabel 上的不同文本?

标签 ios swift uilabel swift3 animatewithduration

目前我的 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/

相关文章:

ios - CustomCell 未反射(reflect)在 UITableView 中

ios - XLForm 推送选择器不工作并且选择器选项未保存为字符串值

ios - 在 SwiftUI 的弹出窗口中保存核心数据实体会抛出 nilError,而无需再次将 .environment 传递给 SubView

ios - 如何子类化 UILabel?

ios - 在 UITableViewCell 内动画 UILabel 文本更改

iphone - iOS 如何在使用 UIApplication openURL 启动浏览器后退出当前应用程序

iphone - View 的底部在模拟器和设备上被切断(单 View 应用程序)

swift - 在 Swift 中定义之前可见的全局变量

Ios swift TableView Cell Label LineBreak 不工作......

ios - uitableview cell cellForRowAtindexPath 从 swift3 中的名称中获取第一个字母