ios - UICollectionView 全屏缩放 UICollectionViewCell

标签 ios objective-c uicollectionview zooming fullscreen

如何放大 UICollectionViewCell 以便全屏显示?我已经扩展了 UICollectionViewFlowLayout 并且在我的 View Controller 中当一个单元格被点击时我正在这样做:

CGPoint pointInCollectionView = [gesture locationInView:self.collectionView];
NSIndexPath *selectedIndexPath = [self.collectionView indexPathForItemAtPoint:pointInCollectionView];
UICollectionViewCell *selectedCell = [self.collectionView cellForItemAtIndexPath:selectedIndexPath];

NSLog(@"Selected cell %@", selectedIndexPath);

不太确定从这里去哪里。 UICollectionView 是否应该负责显示放大的单元格?或者我应该创建一个新的 View Controller 来全屏显示单元格的内容(图像)?

最佳答案

我采用了解决方案 here并稍微修改它以使用 Collection View 。我还添加了一个透明的灰色背景来稍微隐藏原始 View (假设图像没有占据整个框架)。

@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, 0);
// OR, if you want to zoom from the tapped point...
CGRect tempPoint = CGRectMake(pointInCollectionView.x, pointInCollectionView.y, 0, 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,
                                                   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;
}

关于ios - UICollectionView 全屏缩放 UICollectionViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24741597/

相关文章:

objective-c - objective-c : How to store indices forming a sequence from middle to middle (indices for array or vector)

ios - UICollectionViewCell 将数据发送到另一个 viewController

ios - Collection View 可区分的数据源单元格消失且未正确调整大小?

ios - 文本下划线颜色与 iOS 中的文本颜色不同

iphone - 如何在从 nib 文件加载的 UITableViewCell 上设置标签?

objective-c - 名为 "a"的对象和名为 "_a"的对象之间是否存在链接?

ios - 无法更改 UICollectionView 上的 backgroundView 属性

ios - 如何将大视频保存到相册?

ios - UIPageViewController/TextKit 计数页面数

iphone - 一行中的多个单元格