我试图获得一个扩展的过渡动画:我有一个UICollectionView,当用户选择一个单元格时,我希望这个单元格扩展到整个屏幕。这种转变是一种推式转换。
我试着得到细胞的位置,然后动画,但我不认为我这样做是正确的。
我有一些课:
UICollectionViewController
的子类UIViewController
的子类UINavigationControllerDelegate
协议UIViewControllerAnimatedTransitioning
协议的实现第一个问题:是否有人有过这样的效果,有一个示例代码,或者知道实现这一目标的正确方法?这真是太棒了
否则:我应该如何在动画类中获得相关的单元格?我需要细胞的框架和子视图。实际上,我有一个selectedCell
属性,我在prepareForSegue
函数中设置了一个CollectionViewController,但我不确定它是否正确
谢谢你的帮忙
发布于 2014-12-04 09:14:01
也许这不是你想要达到的目标,但无论如何,这可能是一个好的开始:
假设您在控制器中初始化了一个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;
}
https://stackoverflow.com/questions/27297601
复制