-
首先,我们生成一个随机数(例如从0到10):
If randomNumber = 0
Animate imageSet0 [here we create an ImageArray and an animate function â please see code below)
Else if randomNumber = 1
Animate imageSet1
Else if randomNumber = 2
Animate imageSet2
And so onâ¦
-
然后我们放置一个
DispatchQueue
等待上述动画完成的计时器(延时等于
animationDuration
),然后重复上述第一步,生成另一个随机数并播放另一个动画集:
DispatchQueue.main.asyncAfter(deadline: .now() + imageView.animationDuration) {
[Insert code that repeats the first step above and generates another random number to play another animation set]
}
-
理论上,这种随机动画可以无限期播放,直到用户经过该场景。
以下是我目前的代码:
func createImageArray(total: Int, imagePrefix: String) -> [UIImage]{
var imageArray: [UIImage] = []
for imageCount in 0..<total {
let imageName = "\(imagePrefix)-\(imageCount).png"
let image = UIImage(named: imageName)!
imageArray.append(image)
}
return imageArray
}
func animate(imageView: UIImageView, images: [UIImage]){
imageView.animationImages = images
imageView.animationDuration = 1.0
imageView.animationRepeatCount = 1
imageView.startAnimating()
DispatchQueue.main.asyncAfter(deadline: .now() + imageView.animationDuration) {
[Create code that repeats the first step above and generates another random number to play another animation set]
}
}