2014-12-04 98 views
0

我想獲得一個擴大的過渡動畫:我有一個UICollectionView,當用戶選擇一個單元我想這個單元展開到整個屏幕。這個過渡是一個Push推移過渡UICollectionViewCell擴大動畫

我試圖獲取單元格位置然後生成動畫,但我認爲我沒有按照正確的方式進行。

我有一些類:

  • CollectionViewController:亞類FinalViewController的UICollectionViewController
  • UIViewController
  • NavigationControllerDelegate的亞類:UINavigationControllerDelegate協議實現
  • 動畫:UIViewControllerAnimatedTransitioning協議來實現

第一個問題:有沒有人得到這種效果,有一個示例代碼或知道達到此目的的正確方法?這將是非常好的

否則:我應該如何在Animator類中獲得關注的單元格?我需要單元格的框架和子視圖。其實我有一個selectedCell屬性,我如果CollectionViewController的prepareForSegue功能設置,但我不知道這是正確的方式

感謝您的幫助

回答

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; 
} 
+0

好,感謝您的幫助,但我寧願更模塊化的解決方案(主要關心縱橫比:)) – Coconuts 2014-12-04 17:29:57

+0

當您設置capturedImView.frame時,只需保持長寬比... – 2014-12-04 17:36:02

+0

我更新了我的問題:http://stackoverflow.com/questions/27302487/magic移動 - 效果 - 自定義轉換你有什麼想法嗎? – Coconuts 2014-12-06 13:18:52