如何为项目内不同组的多个图像设置动画?

时间:2018-05-21 10:37:08

标签: ios swift uiviewanimation

大家。我的项目中有两个小组。让他们被称为" 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!

}

0 个答案:

没有答案