代码之家  ›  专栏  ›  技术社区  ›  aleksy.t

如何在项目中设置来自不同组的多个图像的动画?

  •  0
  • aleksy.t  · 技术社区  · 6 年前

    所有人。我的项目中有两个组。把它们叫做“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!
    
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Abdelahad Darwish    6 年前

    你可以用 DispatchQueue.main.asyncAfter UIImage.animatedImage(with

    你可以这样使用它:

    self.animatingImage = UIImage.animatedImage(with: one, duration: 1)
    
          // animate after 1 sec 
    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
         self.animatingImage = UIImage.animatedImage(with: two, duration: 1)
    }