Ios 如何在Swift中设置置乱字母的动画?
我想创建一个动画,在动画中,随机字母在它的位置上打乱,并在一个特定的字母处停止。 这可以通过使用UIView.animate实现吗? 下面给出的是一个粗略的想法与工作Ios 如何在Swift中设置置乱字母的动画?,ios,swift,uikit,Ios,Swift,Uikit,我想创建一个动画,在动画中,随机字母在它的位置上打乱,并在一个特定的字母处停止。 这可以通过使用UIView.animate实现吗? 下面给出的是一个粗略的想法与工作 我真的很喜欢这个灵感。仍有两种方法可以将其放入您的应用程序中: 1) 下面是我为您的实例创建的代码: 导入UIKit class ViewController: UIViewController { let listOfRandomLetters = ["@", "%", "*", "^", "1", "2", "3",
我真的很喜欢这个灵感。仍有两种方法可以将其放入您的应用程序中: 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) 使用乐蒂和后效进行更定制的过渡。如果您对以下内容感兴趣,请告诉我:此处链接:。如果我对该代码编写的答案只是随机排列字母,长度不同,但无法随机形成单词,这样可以吗?我真的很喜欢这个主意。你能检查一下这两个链接并告诉我你是否想要这样的东西吗@AaronZheng谢谢,是的,这会有所帮助。不知道是否可以使用CADisplayLink而不是计时器来完成这项工作!