2016-07-28 74 views
0

Animation添加動畫序列中迅速

我有一個集合視圖工作,我有以下代碼到每一個細胞動畫與CATransform3DRotate,在使用較小的延遲是執行

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as? RotationCollectionViewCell else { 

     fatalError("\(String(RotationCollectionViewCell)) not found") 
    } 

    self.addAnimationToCells(cell) 

    return cell 
} 

這是動畫細胞,並創建延遲

func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) { 
    self.delayTime += 0.05 
    print(delayTime, "WILL DISPLAY DELAY TIME") 
} 

private func addAnimationToCells(cell : UICollectionViewCell) { 


     let singleCell: UICollectionViewCell = cell 
     let layer: CALayer = singleCell.layer 

     layer.anchorPoint = CGPointMake(0.5, 0.0) 

     var transform = CATransform3DIdentity; 
     transform.m34 = 1.0/500.0; 
     transform = CATransform3DRotate(transform, CGFloat(90 * M_PI/180), 1, 0, 0) 
     singleCell.layer.transform = transform 

     let start:CGFloat = 90 
     let end:CGFloat = 0 

     let rotationAnimation: CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.x") 
     rotationAnimation.fromValue = start.degreesToRadians 
     rotationAnimation.toValue = end.degreesToRadians 
     rotationAnimation.duration = 0.5 
     rotationAnimation.repeatCount = 0 
     rotationAnimation.removedOnCompletion = false 
     rotationAnimation.fillMode = kCAFillModeForwards 
     rotationAnimation.delegate = CoreAnimationListener { 
      finished in 

      singleCell.layer.transform = CATransform3DIdentity 

      singleCell.layer.removeAllAnimations() 
     } 

     print(delayTime, "THIS IS MY DELAY TIME") 

     delay(delayTime){ 
      cell.layer.addAnimation(rotationAnimation, forKey: "transform.rotation.x") 
     } 
} 

問題是,如果我滾動我有一個空白。我認爲是因爲最後一個被加載的單元格添加了累加延遲的動畫,所以當我滾動它需要一點動畫。我無法弄清楚如何避免在加載時返回最後一個單元格的時間延遲。請幫助

+1

你能重置scrollViewDidEndDecelerating延遲? – richy

+0

有趣的,在這種情況下,我想會在滾動之前...但是一個有趣的想法...我會嘗試 –

+0

沒有工作..必須有一個更好的方法來動畫collectionView單元格的順序.. –

回答

0

爲@Beyowulf評論我只有到delayTime重置爲0 ViewDidAppear