UICollectionViewCell可以轻松翻转和增长

nic*_*nes 10 ios uicollectionview

如何实现动画,其中UICollectionViewCell具有翻转和增长以显示模态视图?

Zed*_*nem 7

这是我在另一个项目中使用的,它运行良好:

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    if (cell.selected) {
        [collectionView deselectItemAtIndexPath:indexPath animated:YES];
        [UIView transitionWithView:cell
                          duration:0.2
                           options:UIViewAnimationOptionTransitionFlipFromLeft
                        animations:^{
                            [cell setFrame:self.selectedCellDefaultFrame];
                            cell.transform = self.selectedCellDefaultTransform;
                        }
                        completion:^(BOOL finished) {
                            self.selectedCellDefaultFrame = CGRectZero;
                            [collectionView reloadItemsAtIndexPaths:@[indexPath]];
                        }];
        return NO;
    }
    else {
        return YES;
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    [cell.superview bringSubviewToFront:cell];
    self.selectedCellDefaultFrame = cell.frame;
    self.selectedCellDefaultTransform = cell.transform;
    [UIView transitionWithView:cell
                      duration:0.2
                       options:UIViewAnimationOptionTransitionFlipFromRight
                    animations:^{
                        [cell setFrame:collectionView.bounds];
                        cell.transform = CGAffineTransformMakeRotation(0.0);
                    }
                    completion:^(BOOL finished) {}];
}
Run Code Online (Sandbox Code Playgroud)

这里不同的事情:

  • bringSubviewToFront:消息呼叫被用来防止该小区的其他小区后面进行动画
  • 我们使用在控制器中声明的两个属性:selectedCellDefaultFrameselectedCellDefaultTransform保存单元的默认状态,并在取消选择时重新初始化它
  • 取消选择时,我们调用reloadItemsAtIndexPaths:方法UICollectionView确保位置的重置完全完成

如果您对此有任何疑问,请告诉我.

祝好运,


Jan*_*edi 4

我还没有尝试过生长动画,但我想我可以帮助 UICollectionViewCell 翻转动画。

尝试:

UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];

[UIView animateWithDuration:1.0
                      delay:0
                    options:(UIViewAnimationOptionAllowUserInteraction)
                 animations:^
                {
                     NSLog(@"starting animation");

                    [UIView transitionFromView:cell.contentView
                                        toView:newView
                                      duration:.5
                                       options:UIViewAnimationOptionTransitionFlipFromRight
                                    completion:nil];
                }
                 completion:^(BOOL finished)
                {
                     NSLog(@"animation end");
                }
 ];
Run Code Online (Sandbox Code Playgroud)

希望有帮助!