简体   繁体   English

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

[英]How do I expand image to full view when being tapped in a UICollectionView?

In my project I'm using a UICollectionView for displaying images and I'm also using UIImageView in UICollectionViewCell . 在我的项目中,我使用UICollectionView显示图像,我也在UICollectionViewCell使用UIImageView All I want is when I tap on a image it should expand and show on fullscreen. 我想要的只是当我点击图像时它应该展开并在全屏显示。 For this I've created a new view takes a UIImageView on it and apply TapGesture on UICollectionViewCells . 为此,我创建了一个新视图,在其上采用UIImageView并在UICollectionViewCells上应用UICollectionViewCells The expanded image open in the next view but isn't presented in the frame I give it. 展开的图像在下一个视图中打开,但未在我给出的框架中显示。 and also when i click on any pic it removed from the collection view. 而当我点击任何图片时,它从集合视图中删除。 please tell me how to reload UICollectionViewCells 请告诉我如何重新加载UICollectionViewCells 在此输入图像描述 Please suggest me how to put image on the prescribed frames. 请建议我如何将图像放在规定的框架上。 I'm sharing my code 我正在分享我的代码

- (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];  
}

A simpler way to achieve this would be to push to a new view that you display the full screen picture, instead of adding it as a subview. 实现这一目标的一种更简单的方法是推送到显示全屏图片的新视图,而不是将其添加为子视图。

You can also customize the transition animation of the navigation controller to fit your needs. 您还可以自定义导航控制器的过渡动画以满足您的需要。

Ok don't take anything for granted, because one thing im sure of, is that looking at that code made me confused. 好吧,不要把任何事情视为理所当然,因为有一件事我确定,看着那段代码让我感到困惑。 But here is how i see it, and what went wrong: 但这是我如何看待它,以及出了什么问题:

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

With those 2 lines you are taking out your view from cell and putting it in this new view1. 使用这两行,您将从单元格中取出视图并将其放入此新视图1中。 So just allocating new UIImageVIew and setting its image could solve it. 因此,只需分配新的UIImageVIew并设置其图像就可以解决它。 And some more suggestions: 还有一些建议:

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

Here that first line of code isn't doing anything. 这里第一行代码没有做任何事情。 You set object, and then you set that object again. 您设置对象,然后再次设置该对象。 Next thing is do not add subviews in cellForItemAtIndexPath without removing previously added. 接下来就是不要在不删除以前添加的内容的情况下在cellForItemAtIndexPath中添加子视图。 When dequeuing cells, you are using the same cells over and over again, so you will creat memory leak by stacking views on top of each other. 当单元格出列时,您反复使用相同的单元格,因此您将通过将视图堆叠在一起来创建内存泄漏。 You could even omit all that adding stuff by creating custom cell with its own UIImageView. 您甚至可以通过使用自己的UIImageView创建自定义单元格来省略所有添加内容。 Next there is no need to use gesture recognition, CollectionView have this one delegate: 接下来不需要使用手势识别,CollectionView有这个代表:

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

You can replace your expandImage method with it, inside you would just simply get your image from imagearray . 你可以用它替换你的expandImage方法,你只需从imagearray中获取你的图像。 And lastly there's this beautiful library ready to use for you, MHFacebookImageViewer . 最后,这个美丽的图书馆随时可供您使用, MHFacebookImageViewer

So it is better to add view in a window.so it is display in full screen.Use Following Code. 因此最好在窗口中添加视图。因此它会全屏显示。使用以下代码。

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];

Add imageview on navigationController.view navigationController.view上添加imageview

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

i done the above by this code firstly added tap gesture then apply the following code 我通过此代码完成上述操作,首先添加了tap手势,然后应用以下代码

-(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];
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 触摸后如何将图像扩展为全屏? - How do I make an image expand to full screen when touched? 双击集合视图单元格时如何使图像可见? - How do I make image visible when collection view cell is double tapped? UICollectionView 被点击时如何居中? - How to center a UICollectionView when it is tapped? 如何点击时更改按钮的图像 - Swift - How do I change the image of a button when tapped - Swift 在 SwiftUI 中点击 watchOS 应用程序时如何更改图像 - How do I change image when tapped on it watchOS app in SwiftUI 如何在点击UIActionSheet中的选项时显示模式视图? - How do I present a modal view upon an option in UIActionSheet being tapped? 当我们点击图像时,如何以全屏方式缩放图像 - How to zoom the image along with full screen when we tapped on it 我在视图控制器中显示了一个UIView,如何检测用户何时在此视图外部轻敲? - I have a UIView presented in a view controller, how do I detect when the user tapped outside of this view? 当我们在Ios中点击它时,如何使用图像进行集合视图单元格“放大”和“缩小” - How to do collection view cell “zoom in” and “zoom out” with image when we tapped on it in Ios 轻按图像时,如何在phonegap / cordova上禁用图像缩放? - How do I disable image zooming on phonegap/cordova when image is tapped?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM