也許這不是你想實現它的方式,但無論如何這可能是一個好開始:
假設你有一個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;
}
好,感謝您的幫助,但我寧願更模塊化的解決方案(主要關心縱橫比:)) – Coconuts 2014-12-04 17:29:57
當您設置capturedImView.frame時,只需保持長寬比... – 2014-12-04 17:36:02
我更新了我的問題:http://stackoverflow.com/questions/27302487/magic移動 - 效果 - 自定義轉換你有什麼想法嗎? – Coconuts 2014-12-06 13:18:52