UICollectionViewCell扩展动画

时间:2014-12-04 15:21:41

标签: ios animation swift uicollectionview uicollectionviewcell

我正在尝试扩展过渡动画:我有一个UICollectionView,当用户选择一个单元格时,我希望这个单元格扩展到整个屏幕。过渡是Push segue过渡

我尝试获取单元格位置然后制作动画,但我不认为我正在以正确的方式进行。

我有一些课程:

  • CollectionViewController:UICollectionViewController
  • 的子类
  • FinalViewController:UIViewController
  • 的子类
  • NavigationControllerDelegate:UINavigationControllerDelegate已实施协议
  • Animator:已实施UIViewControllerAnimatedTransitioning协议

第一个问题:有没有人得到这种效果,有一个示例代码或知道一个正确的方法来实现这一目标?这真的很棒

否则:我应该如何获得Animator类中的相关单元格?我需要单元格的框架和子视图。实际上我有一个selectedCell属性,我在prepareForSegue函数中设置了如果是CollectionViewController,但我不确定它是正确的方式

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

也许这不是你想要的方式,但无论如何它可能是一个好的开始:

假设您的控制器中已初始化UIImageView *capturedImView;。然后,当您选择一个单元格时,您可以尝试:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    // Remove the image view (you can do it in a better place)
    [capturedImView removeFromSuperview];

    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];

    // Get the frame of the selected cell in your current view :)
    CGRect frame = [collectionView convertRect:cell.frame toView:self.view];

    // set the capturedImView
    capturedImView.frame = frame;
    UIImage *snapshotImg = [self captureScreenInRect:frame forView:cell];
    capturedImView.image = snapshotImg;
    [self.view addSubview:capturedImView];

    // Play the animation
    [UIView animateWithDuration:3 animations:^{
        // Here we don't care about aspect ratio :s
        capturedImView.frame = self.view.frame;
    }];
}

其中captureScreenInRect:forView:获取传递参数的视图的快照:

- (UIImage *)captureScreenInRect:(CGRect)captureFrame forView:(UIView*)view{
    CALayer *layer;
    layer = view.layer;
    UIGraphicsBeginImageContext(captureFrame.size);
    CGContextClipToRect (UIGraphicsGetCurrentContext(),CGRectMake(0, 0, captureFrame.size.width, captureFrame.size.height));
    [layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *captureImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return captureImage;
}