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

Swift UIView.animate between many different strings

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

我的问题是我有大约 20 多个字符串我想在它们之间淡入淡出 in/out 但我不相信创建 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 淡化标签,你可以在 Whosebug 上找到它的一个例子。然后我创建了一个计时器,它将 运行 淡入淡出动画并使用一个标签

循环遍历字符串

你可以用这个实现它:

    Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true) { (timer) in
        UIView.animate(withDuration: 1.0, animations: {
            self.label.alpha = 0
        }, completion: { (finished) in
            UIView.animate(withDuration: 1.0, animations: {
                self.label.alpha = 1.0

                let text = self.quotes.remove(at: 0)
                self.label.text = text
                self.quotes.insert(text, at: self.quotes.count-1)
            })
        })

    }

您可以使用 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)
        })

    }
}

@bigubosu 这应该有效:

var strings = ["1", "2", "3", "4", "5", "6"]

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    animate()
}

func animate() {
    guard strings.count > 0 else { return }

    let alpha:CGFloat = textLabel.alpha == 0 ? 1 : 0

    if alpha == 1 {
        textLabel.text = strings.remove(at: 0)
    }

    UIView.animate(withDuration: 2.0, animations: { [weak self] in
        self?.textLabel.alpha = alpha
    }, completion: { [weak self]  (finished) in
        self?.animate()
    })
}