ios – 如何在Swift的位置上制作扰乱字母的动画?

我想创建一个动画,其中随机字母在其位置加扰并停在特定字母处.

可以使用UIView.animate实现吗?

下面给出了一个粗略的想法.

《ios – 如何在Swift的位置上制作扰乱字母的动画?》

最佳答案 真的很喜欢这个灵感.还有两种方法可以将其添加到您的应用中:

1)这是我为您的实例创建的代码:

导入UIKit

class ViewController: UIViewController {
    let listOfRandomLetters = ["@", "%", "*", "^", "1", "2", "3", " ", " "]
    var textNeedDisplaying = ["String", "Other Person", "Sample", "String", "Other Person", "Sample", "String", "Other Person", "Sample", "String", "Other Person", "Sample", "String", "Other Person", "Sample", "String", "Other Person", "Sample", "String", "Other Person", "Sample"]
    var newList: [String] = []
    var incrementer = 0
    var internalTimer: Timer?
    var timer: Timer?
    var mainTimer: Timer?
    @IBOutlet weak var animatingLabel: UILabel!
    override func viewDidAppear(_ animated: Bool) {
        schedule()
    }

    func schedule() {
        //Main Timer interval usually adds up the other two intervals
        self.mainTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { _ in
            //Play around with the time Intervals
            self.internalTimer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true, block: { _ in
                for _ in 0...arc4random_uniform(UInt32(10)) + 1 {
                    let randomNumber = arc4random_uniform(UInt32(self.listOfRandomLetters.count - 1))
                    self.newList.append(self.listOfRandomLetters[Int(randomNumber)])
                }
                self.animatingLabel.text = self.newList.joined()
                self.newList.removeAll()
            })
            //Play around with the time Intervals
            self.timer = Timer.scheduledTimer(withTimeInterval: 0.7, repeats: false, block: { _ in
                if self.incrementer != self.textNeedDisplaying.count - 1 {
                    self.internalTimer?.invalidate()
                    self.animatingLabel.text = self.textNeedDisplaying[self.incrementer]
                    self.incrementer += 1

                } else {
                    self.timer?.invalidate()
                    self.internalTimer?.invalidate()
                    self.mainTimer?.invalidate()
                    self.animatingLabel.text = "DONE"
                }
            })
        })
    }
}

如果你想让各个字母都有动画让我知道.我会做到这一点……确保标签的宽度约束足够大以适应你的话.此外,如果代码可以更简洁或更好,请告诉我.

2)使用Lottie和After Effects进行更加个性化的过渡.如果您对此感兴趣,请告诉我:链接:https://airbnb.design/lottie/.

点赞