ios - 许多不同字符串之间的 Swift UIView.animate

标签 ios swift uiviewanimation

我希望在从新设备启动后重新创建 iPhone 上出现的欢迎屏幕上的动画 - 您在这里看到的各种不同语言的问候语 - https://www.youtube.com/watch?v=YZDbGyvAK7o

我的问题是我有大约 20 多个字符串我想在它们之间淡入/淡出,但我不相信创建 20 多个不同的 UILabel 是解决这个问题的最有效方法。必须有另一种方法来执行此操作,而不是对其进行硬编码。

这是到目前为止我对关键帧的了解

//textC UIlabel declaration and initialising its' properties.   
UIView.animateKeyframes(withDuration: total, delay: 0, options: [.calculationModeLinear, .repeat], animations: {

    UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1/total, animations: {
                let attributedStrD = NSMutableAttributedString(string: countryArr[0], attributes: resultAttributes as [NSAttributedStringKey : Any])
                print(attributedStrD.string)
                textC.attributedText = attributedStrD

            })

    UIView.addKeyframe(withRelativeStartTime: 1/total, relativeDuration: 1/total, animations: {
                let attributedStrD = NSMutableAttributedString(string: countryArr[0], attributes: resultAttributes as [NSAttributedStringKey : Any])
                textC.alpha = 0
                textC.attributedText = attributedStrD

            })
    UIView.addKeyframe(withRelativeStartTime: 2/total, relativeDuration: 1/total, animations: {
                textC.alpha = 1
                let attributedStrD = NSMutableAttributedString(string: countryArr[1], attributes: resultAttributes as [NSAttributedStringKey : Any])
                textC.attributedText = attributedStrD
                print("2")
            })
    UIView.addKeyframe(withRelativeStartTime: 3/total, relativeDuration: 1/total, animations: {
                textC.alpha = 0

                let attributedStrD = NSMutableAttributedString(string: countryArr[1], attributes: resultAttributes as [NSAttributedStringKey : Any])
                textC.attributedText = attributedStrD
            })

    UIView.addKeyframe(withRelativeStartTime: 4/total, relativeDuration: 1/total, animations: {
                textC.alpha = 1

                let attributedStrD = NSMutableAttributedString(string: countryArr[2], attributes: resultAttributes as [NSAttributedStringKey : Any])
                textC.attributedText = attributedStrD
            })

    UIView.addKeyframe(withRelativeStartTime: 5/total, relativeDuration: 1/total, animations: {
                textC.alpha = 0

                let attributedStrD = NSMutableAttributedString(string: countryArr[2], attributes: resultAttributes as [NSAttributedStringKey : Any])
        textC.attributedText = attributedStrD
            })
        }) { (_) in
             print("done animation")
        }

这也不太有效,因为文本没有更新,而是在模拟器上构建和运行时看到数组中的最后一个字符串。

更新: 虽然我想避免使用计时器,但这似乎是唯一适合我的方法。所以我做了一个 UIView 扩展,它使用 CATransitionView 淡化标签,你可以在 stackoverflow 上找到它的一个例子。然后我创建了一个计时器,它将运行淡入淡出动画并使用一个标签循环遍历字符串

最佳答案

您可以使用 UIView.transition 在单个 UILabel 中交叉淡入淡出文本,使用 Timer 启动下一个动画:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.textLabel.text = self.strings[stringIndex]

    let _ = Timer.scheduledTimer(withTimeInterval: 3.0, repeats: true) { (timer) in
        self.stringIndex = (self.stringIndex + 1) % self.strings.count

        UIView.transition(with: self.textLabel, duration: 0.8, options: .transitionCrossDissolve, animations: {
            self.textLabel.text = ""
        }, completion: { (Void) in
            UIView.transition(with: self.textLabel, duration: 0.8, options: .transitionCrossDissolve, animations: {
                self.textLabel.text = self.strings[self.stringIndex]
            }, completion: nil)
        })

    }
}

enter image description here

关于ios - 许多不同字符串之间的 Swift UIView.animate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52752813/

相关文章:

ios - OpenGL ES 2.0-纹理四边形

swift - 何时使用设备以及何时使用 Metal 着色语言中的常量地址空间限定符?

objective-c - 检测键盘动画有多远?

ios - 如何制作 secret 的 iOS 应用程序文本动画

c# - 调用 method_getImplementation 在设备上抛出 ExecutionEngineException

ios - Xcode 6.4 导出临时 "Session has expired"

javascript - React Native 发布构建崩溃问题,但在调试时它工作正常

swift - 使用 Stormpath 登录 LinkedIn

php - 将一组 ID 从 Swift URLSession 传递到 PHP 以进行 SQL 查询?

ios - 在 UIView.animateWithDuration 的完成 block 中添加 View