objective c "cell for item at index path" 重新加载时的 UICollectionViewCell 动画和过渡效果

UICollection View Cell Animation and transistion effects on reload in "cell for item at index path" in objective c

我有集合视图和六个单元格,每个单元格都有图像,它会出现类似封面垂直效果的动画。但我需要每个单元格都来自具有动画时间延迟的封面垂直效果。 例如如果一个cell来自cover vertical(它来自bottom to top effect)with time delay 0.2表示second cell来自0.6的时间延迟同样它必须 来。这就是我需要的。请帮助我。

试试这个:

var isCollectionViewScrollUp: Bool = true


func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    let collectionHeight: CGFloat = self.collectionView!.bounds.size.height

    let contentOffsetY: CGFloat = self.collectionView.contentOffset.y
    let contentSizeHeight: CGFloat = self.collectionView.contentSize.height

    var height:CGFloat = 0.0//collectionHeight * CGFloat(indexPath.row)

    if isCollectionViewScrollUp && (contentOffsetY + self.collectionView.frame.size.height) < contentSizeHeight {

        let index = Int(indexPath.row) + Int(1)
        if index % 3 == 1 {
            height = collectionHeight + 300
        }
        else if index % 3 == 2 {
            height = collectionHeight + 300 * 2
        }
        else {
            height = collectionHeight + 300 * 3
        }

        cell.transform = CGAffineTransform(translationX: 0, y: height)
    } else if !isCollectionViewScrollUp && contentOffsetY > 0.0 {
        let index = Int(indexPath.row) + Int(1)
        if index % 3 == 1 {
            height = collectionHeight + 300 * 3
        }
        else if index % 3 == 2 {
            height = collectionHeight + 300 * 2
        }
        else {
            height = collectionHeight + 300
        }

        cell.transform = CGAffineTransform(translationX: 0, y: -height)
    }

    UIView.animate(withDuration: 1, delay: 0.03, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
        cell.transform = CGAffineTransform(translationX: 0, y: 0);
    }, completion: nil)

}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let scrollVelocity:CGPoint  = self.collectionView.panGestureRecognizer.velocity(in: self.collectionView?.superview)
    if scrollVelocity.y > 0.0 { //ScrollDown
        isCollectionViewScrollUp = false
    } else if scrollVelocity.y < 0.0 { //ScrollUp
        isCollectionViewScrollUp = true
    }
}
- (void)collectionView:(UICollectionView *)collectionView
   willDisplayCell:(UICollectionViewCell *)cell
forItemAtIndexPath:(NSIndexPath *)indexPath{

CGFloat collectionHeight = self.CollectionView.bounds.size.height;
CGFloat contentOffsetY = self.CollectionView.contentOffset.y;
CGFloat contentSizeHeight = self.CollectionView.contentSize.height;
CGFloat height = 0.0;

if(isCollectionViewScrollUp && contentOffsetY + self.CollectionView.frame.size.height < contentSizeHeight) {

    int index = (int)indexPath.row + 1;

    if (index % 3 == 1){
        height = collectionHeight + 300;
    }else if(index % 3 == 2){
        height = collectionHeight + 300 * 2;

    }else{
        height = collectionHeight + 300 * 3;
    }
    cell.transform = CGAffineTransformMakeTranslation(0, height);

}else if(!isCollectionViewScrollUp && contentOffsetY > 0.0){

    int index = (int)indexPath.row + 1;

    if(index % 3 == 1){
        height = collectionHeight  + 300 * 3;
    }else if(index % 3 == 2){
        height = collectionHeight + 300 * 2;
    }else{
        height = collectionHeight + 300;
    }

    cell.transform = CGAffineTransformMakeTranslation(0, -height);
}

[UIView animateWithDuration:1 delay:0.03 usingSpringWithDamping:0.8 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseIn animations:^{

    cell.transform = CGAffineTransformMakeTranslation(0, 0);

} completion:nil]; 
}  

-(void) scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint scrollVelocity = [CollectionView.panGestureRecognizer velocityInView:CollectionView.superview];
if (scrollVelocity.y > 0.0) { //ScrollDown
    isCollectionViewScrollUp = NO;
} else if (scrollVelocity.y < 0.0 ){ //ScrollUp
    isCollectionViewScrollUp = YES;
}
}