在UICollectionView中点击时如何将图像扩展为完整视图?

时间:2015-10-07 06:30:04

标签: ios objective-c uiimageview uicollectionview uicollectionviewcell

在我的项目中,我使用UICollectionView来显示图片,我还在UIImageView中使用UICollectionViewCell。 我想要的只是当我点击图像时它应该展开并在全屏显示。 为此,我创建了一个新视图,其上需要UIImageView并在UICollectionViewCells上应用TapGesture。 展开的图像在下一个视图中打开,但不会在我给出的框架中显示。 而当我点击任何图片时,它从集合视图中删除。请告诉我如何重新加载UICollectionViewCells enter image description here 请建议我如何将图像放在规定的框架上。 我正在分享我的代码

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

    imgview = (UIImageView *)[cell viewWithTag:100];
    imgview = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,160, 160)];

    imgview.image = [UIImage imageNamed:[imagearray objectAtIndex:indexPath.row]];
    //cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:[imagearray objectAtIndex:indexPath.row]]];
    [cell.contentView addSubview:imgview];

    UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(expandImage:)];
    tapped.numberOfTapsRequired = 1;
    [imgview setUserInteractionEnabled:YES];
    imgview.tag = indexPath.row;
    [imgview addGestureRecognizer:tapped];

    [cell.contentView addSubview:imgview];

    return cell;
}

-(void)expandImage:(UITapGestureRecognizer*)recogniser
{
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width , self.view.frame.size.height)];
    view1.backgroundColor = [UIColor greenColor];
    [self.view addSubview:view1];

    UIButton *closeButton = [[UIButton alloc]initWithFrame:CGRectMake(310, 10, 50, 40)];
    [closeButton setTitle:@"Close" forState:UIControlStateNormal];
    [closeButton addTarget:self action:@selector(Closetab) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:closeButton];

    UIImageView *photoView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 70, self.view.frame.size.width, self.view.frame.size.height-200)];
    [photoView setBackgroundColor:[UIColor blueColor]];
    [view1 addSubview:photoView];
    photoView.accessibilityIdentifier = @"nature2.png";

    UIImageView *photoView1 = (UIImageView*)recogniser.view;
    photoView1.accessibilityIdentifier = @"nature2.png";

    //photoView.clipsToBounds = YES;

    // photoView.accessibilityIdentifier = @"apple1.png";

    photoView1.contentMode =  UIViewContentModeScaleAspectFill;

    //photoView1.clipsToBounds = YES;
    [view1 addSubview:photoView1];  
}

5 个答案:

答案 0 :(得分:1)

实现这一目标的一种更简单的方法是推送到显示全屏图片的新视图,而不是将其添加为子视图。

您还可以自定义导航控制器的过渡动画以满足您的需求。

答案 1 :(得分:1)

好的,不要把任何事情视为理所当然,因为有一件事我确定,看着那段代码让我感到困惑。但这是我如何看待它,以及出了什么问题:

    UIImageView *photoView1 = (UIImageView*)recogniser.view;
    [view1 addSubview:photoView1]; 

使用这两行,您将从单元格中取出视图并将其放入此新视图中1。 因此,只需分配新的UIImageVIew并设置其图像就可以解决它。 还有一些建议:

    imgview = (UIImageView *)[cell viewWithTag:100];
    imgview = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,160, 160)];

这里第一行代码没有做任何事情。您设置对象,然后再次设置该对象。 接下来就是不要在 cellForItemAtIndexPath 中添加子视图而不删除之前添加的子视图。当单元格出列时,您反复使用相同的单元格,因此您将通过将视图堆叠在一起来创建内存泄漏。您甚至可以通过使用自己的UIImageView创建自定义单元格来省略所有添加内容。接下来不需要使用手势识别,CollectionView有这个代表:

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

您可以用它替换 expandImage 方法,只需从 imagearray 获取图片即可。 最后,这个美丽的图书馆随时可供你使用,MHFacebookImageViewer

答案 2 :(得分:1)

所以最好在窗口中添加视图。因此它会全屏显示。使用以下代码。

UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width , self.view.frame.size.height)];
    view1.backgroundColor = [UIColor greenColor];
    [self.view.window addSubview:view1];

答案 3 :(得分:1)

imageview

上添加navigationController.view
UIImageView *imgView =[[UIImageView alloc]initWithFrame:self.view.bounds];
[self.navigationController.view addSubview:imgView];

答案 4 :(得分:0)

我通过此代码完成了上述操作 首先添加了点击手势 然后应用以下代码

-(void)expandImage:(UITapGestureRecognizer*)recogniser
{

    view1.hidden = NO;

    UIImageView *photoView1 = (UIImageView*)recogniser.view;
    photoView1.frame = CGRectMake(0, 0, self.view.frame.size.width, 510);
    photoView1.accessibilityIdentifier = @"nature2.png";

    //photoView.clipsToBounds = YES;

    // photoView.accessibilityIdentifier = @"apple1.png";

    photoView1.contentMode =  UIViewContentModeScaleAspectFill;

    photoView1.clipsToBounds = YES;

    [view1 addSubview:photoView1];
    }