ios – UICollectionView全屏放大UICollectionViewCell

前端之家收集整理的这篇文章主要介绍了ios – UICollectionView全屏放大UICollectionViewCell前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何放大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;
}

猜你在找的iOS相关文章