[英]How do I expand image to full view when being tapped in a UICollectionView?
在我的項目中,我使用UICollectionView
顯示圖像,我也在UICollectionViewCell
使用UIImageView
。 我想要的只是當我點擊圖像時它應該展開並在全屏顯示。 為此,我創建了一個新視圖,在其上采用UIImageView
並在UICollectionViewCells
上應用UICollectionViewCells
。 展開的圖像在下一個視圖中打開,但未在我給出的框架中顯示。 而當我點擊任何圖片時,它從集合視圖中刪除。 請告訴我如何重新加載UICollectionViewCells
請建議我如何將圖像放在規定的框架上。 我正在分享我的代碼
- (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];
}
實現這一目標的一種更簡單的方法是推送到顯示全屏圖片的新視圖,而不是將其添加為子視圖。
您還可以自定義導航控制器的過渡動畫以滿足您的需要。
好吧,不要把任何事情視為理所當然,因為有一件事我確定,看着那段代碼讓我感到困惑。 但這是我如何看待它,以及出了什么問題:
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 。
因此最好在窗口中添加視圖。因此它會全屏顯示。使用以下代碼。
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];
在navigationController.view
上添加imageview
UIImageView *imgView =[[UIImageView alloc]initWithFrame:self.view.bounds];
[self.navigationController.view addSubview:imgView];
我通過此代碼完成上述操作,首先添加了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.