2016-11-30 129 views
0

以下塊是我的代碼。我不知道爲什麼,但動畫延遲根本不起作用。無論多少秒我增加它..動畫無法正常工作(Swift 3)

func changeLabelisTapped(_ textLabel: UILabel, tapGesture: UITapGestureRecognizer) { 
    for i in 0...2{ 
     UIView.animate(withDuration: 2, delay: Double(i)+2, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseOut, animations: ({ 
      self.subArray.remove(at: 0) 
      self.collectionView.deleteItems(at: [IndexPath(row: 0, section: 0)]) 
     }), completion: nil) 
    } 
    let tempArray = Array(self.tabledData[3...self.tabledData.count-1]) 
    print(tempArray.count) 
    for j in 0...2{ 
     UIView.animate(withDuration: 2, delay: 2, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseOut, animations: ({ 
      let newElement = tempArray[j] 
      self.subArray.append(newElement) 
      self.collectionView.insertItems(at:[IndexPath(row: j, section: 0)]) 
     }), completion: nil) 
    } 
} 

回答

1

UIView動畫不排隊。所以每次你通過一個循環來處理它時,下一個動畫會取消之前的動畫,而你看不到延遲。不要使用循環,而應該鏈接動畫,以便在完成處理程序中調用下一個動畫。刪除循環並調用完成處理程序中發生的下一個動畫。

var currentCount = 0 

    UIView.animate(withDuration: 2, delay: 2, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseOut, animations: ({ 
     let newElement = tempArray[self.currentCount] 
     self.subArray.append(newElement) 
     self.collectionView.insertItems(at:[IndexPath(row: self.currentCount, section: 0)]) 
    }), completion: { _ in 
     //next animation follows here 
     self.currentCount += 1 
     UIView.animate(withDuration: 2, delay: 2, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseOut, animations: ({ 
     let newElement = tempArray[self.currentCount] 
     self.subArray.append(newElement) 
     self.collectionView.insertItems(at:[IndexPath(row: self.currentCount, section: 0)]) 
    }), completion: { _ in //and so on 

    }) 

    }) 

如果您想幹淨地控制動畫發生的次數,那麼您可以在完成處理程序中使用遞歸回調。要做到這一點,請將您的動畫代碼移到一個函數中。

let loopCount = 3 

    var currentCount = 0 

    func animateMyLabel() { 
    UIView.animate(withDuration: 2, delay: 2, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseOut, animations: ({ 
     let newElement = tempArray[self.currentCount] 
     self.subArray.append(newElement) 
     self.collectionView.insertItems(at:[IndexPath(row: self.currentCount, section: 0)]) 
    }), completion: { _ in 
     if self.currentCount < self.loopCount { 
      self.currentCount += 1 
      //call the animation again 
      self.animateMyLabel() 
     } 

    }) 

    } 

編輯

已經面臨目前的一個項目,我做了類似的延遲問題,我學到了新的方式來強制延遲是否如預期般可能幫助任何人是不工作的是遇到這個問題。解決方案是通過添加延遲(秒:n){}來強制在完成處理程序中延遲下一個動畫,其中n是延遲應發生的秒數。所以,把它添加到這個問題:

let loopCount = 3 

    var currentCount = 0 

    func animateMyLabel() { 
    UIView.animate(withDuration: 2, delay: 2, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseOut, animations: ({ 
     let newElement = tempArray[self.currentCount] 
     self.subArray.append(newElement) 
     self.collectionView.insertItems(at:[IndexPath(row: self.currentCount, section: 0)]) 
    }), completion: { _ in 
     if self.currentCount < self.loopCount { 
      //add the delay here 
      delay(seconds: 2) { 
      self.currentCount += 1 
      //call the animation again 
      self.animateMyLabel() 
      } 
     } 

    }) 

    } 

此外,雖然動畫不排隊,可以使用一個循環的動畫提供序列中的下一個動畫有一個延遲,它允許一個動畫來完成。

+0

它仍然無法正常工作..我將延遲增加到了10秒..動畫需要同一時間完成。但謝謝你的回答! – user3608914

+0

您刪除了for循環,但它仍然不起作用? – gwinyai

+0

是的,我嘗試了你的第二塊..它不工作...... :(我看了一個動畫教程...視頻顯示,你不需要完成框來增加延遲..你只是必須增加delayCounter和單元格會相應的佈局。所以,我不知道它是否只是插入和刪除單元格的問題 – user3608914