所有人。我的项目中有两个组。把它们叫做“images1”和“images2”。我想设置第一组中所有图像的动画,然后立即设置ViewController中第二组中所有图像的动画。首先,我创建图像数组,然后使用UIView.animateKeyFrames函数。不幸的是,我只能设置第一个组的动画,当我试图添加另一个关键帧来设置另一个组的动画时,似乎没有任何效果。我还试着把另一个关键帧放在“完成”的位置,但这会导致错误。我的代码:
import UIKit
class ViewController: UIViewController
{
var one: [UIImage] = []
var two: [UIImage] = []
override func viewDidLoad()
{
super.viewDidLoad()
one = createImageArray(total: 20, imagePrefix: "images1")
two = createImageArray(total: 20, imagePrefix: "images2")
UIView.animateKeyframes(withDuration: 3.0, delay: 0.0, options: [], animations: {
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 3, animations: {
self.animatingImage.animationImages = self.one
self.animatingImage.animationDuration = 2
self.animatingImage.startAnimating()
})
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 3, animations: {
self.animatingImage.animationImages = self.two
self.animatingImage.animationDuration = 2
self.animatingImage.startAnimating()
})
}, completion:{ _ in
print("end")
})
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
func createImageArray(total: Int, imagePrefix: String) -> [UIImage]
{
var imageArray: [UIImage] = []
for imageCount in 1...total
{
let imageName = "\(imagePrefix)\(imageCount).jpg"
let image = UIImage(named: "\(imageName)")!
imageArray.append(image)
}
return imageArray
}
@IBOutlet weak var animatingImage: UIImageView!
}