如何放大UICollectionViewCell以便全屏显示?我已经扩展了UICollectionViewFlowLayout并在我的视图控制器中点击了一个单元格,我这样做:
CGPoint pointInCollectionView = [gesture locationInView:self.collectionView]; NSIndexPath *selectedindexPath = [self.collectionView indexPathForItemAtPoint:pointInCollectionView]; UICollectionViewCell *selectedCell = [self.collectionView cellForItemAtIndexPath:selectedindexPath]; NSLog(@"Selected cell %@",selectedindexPath);
不确定从哪里开始. UICollectionView应该负责显示放大的单元格吗?或者我应该创建一个新的视图控制器,以全屏显示单元格(图像)的内容?
解决方法
我采用了解决方案
here并稍微修改了它以使用集合视图.我还添加了一个透明的灰色背景来隐藏原始视图(假设图像不占用整个帧).
@implementation CollectionViewController
{
UIImageView *fullScreenImageView;
UIImageView *originalImageView;
}
...
// in whatever method you're using to detect the cell selection
CGPoint pointInCollectionView = [gesture locationInView:self.collectionView];
NSIndexPath *selectedindexPath = [self.collectionView indexPathForItemAtPoint:pointInCollectionView];
UICollectionViewCell *selectedCell = [self.collectionView cellForItemAtIndexPath:selectedindexPath];
originalImageView = [selectedCell imageView]; // or whatever cell element holds your image that you want to zoom
fullScreenImageView = [[UIImageView alloc] init];
[fullScreenImageView setContentMode:UIViewContentModeScaleAspectFit];
fullScreenImageView.image = [originalImageView image];
// ***********************************************************************************
// You can either use this to zoom in from the center of your cell
CGRect tempPoint = CGRectMake(originalImageView.center.x,originalImageView.center.y,0);
// OR,if you want to zoom from the tapped point...
CGRect tempPoint = CGRectMake(pointInCollectionView.x,pointInCollectionView.y,0);
// ***********************************************************************************
CGRect startingPoint = [self.view convertRect:tempPoint fromView:[self.collectionView cellForItemAtIndexPath:selectedindexPath]];
[fullScreenImageView setFrame:startingPoint];
[fullScreenImageView setBackgroundColor:[[UIColor lightGrayColor] colorWithAlphaComponent:0.9f]];
[self.view addSubview:fullScreenImageView];
[UIView animateWithDuration:0.4
animations:^{
[fullScreenImageView setFrame:CGRectMake(0,self.view.bounds.size.width,self.view.bounds.size.height)];
}];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fullScreenImageViewTapped:)];
singleTap.numberOfTapsrequired = 1;
singleTap.numberOftouchesrequired = 1;
[fullScreenImageView addGestureRecognizer:singleTap];
[fullScreenImageView setUserInteractionEnabled:YES];
...
- (void)fullScreenImageViewTapped:(UIGestureRecognizer *)gestureRecognizer {
CGRect point=[self.view convertRect:originalImageView.bounds fromView:originalImageView];
gestureRecognizer.view.backgroundColor=[UIColor clearColor];
[UIView animateWithDuration:0.5
animations:^{
[(UIImageView *)gestureRecognizer.view setFrame:point];
}];
[self performSelector:@selector(animationDone:) withObject:[gestureRecognizer view] afterDelay:0.4];
}
-(void)animationDone:(UIView *)view
{
[fullScreenImageView removeFromSuperview];
fullScreenImageView = nil;
}