2017-12-02 280 views
0
  1. 首先我們產生一個隨機數(例如從比如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… 
    
  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] 

    } 
} 

回答

0

可以使用UIView.animate(withDuration: delay: options: animations:) API爲。注意延遲參數會讓你在一定延遲後開始動畫。

+0

Hi @Sandeep - 您將如何在隨機函數中使用UIView.animate api播放十個動畫之一? –