0
首先我们产生一个随机数(例如从比如0〜10):如何斯威夫特创建动画的时间延迟3.0
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]
}
}
Hi @Sandeep - 您将如何在随机函数中使用UIView.animate api播放十个动画之一? –