代码之家  ›  专栏  ›  技术社区  ›  Server Programmer

如何在Swift 3.0中通过动画创建延时

  •  0
  • Server Programmer  · 技术社区  · 8 年前
    1. 首先,我们生成一个随机数(例如从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…
      
    2. 然后我们放置一个 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]
      }
      
    3. 理论上,这种随机动画可以无限期播放,直到用户经过该场景。

    以下是我目前的代码:

    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]
    
        }
    }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Sandeep    8 年前

    您可以使用 UIView.animate(withDuration: delay: options: animations:)