如何在CollectionView图像缩放时返回

时间:2014-10-30 14:00:37

标签: ios uicollectionview

这里我选择了缩放Collectionview Cell Image当它被选中但是现在我想在内部图像中触摸时查看然后返回到集合视图如何可能请给我解决方案

这里我的代码是

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
[self zoomToSelectedImage:indexPath];
}
-(void)zoomToSelectedImage:(NSIndexPath *)indexPath
{
NSDictionary *dict = [self.imagesa objectAtIndex:indexPath.item];
NSString *img=[dict valueForKey:@"link"];

UIImageView *zoomImage = [[UIImageView alloc] init];
[zoomImage sd_setImageWithURL:[NSURL URLWithString:img]];
zoomImage.contentMode = UIViewContentModeScaleAspectFit;
zoomImage.backgroundColor=[UIColor grayColor];
zoomImage.userInteractionEnabled=YES;
UIPinchGestureRecognizer *pinchgesture=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGestureDetected:)];
[pinchgesture setDelegate:self];
[zoomImage addGestureRecognizer:pinchgesture];

UIPanGestureRecognizer *pangausture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureDetected:)];
[pangausture setDelegate:self];
[zoomImage addGestureRecognizer:pangausture];
self.imagecollection.hidden=TRUE;


CGRect zoomFrameTo = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height);
UICollectionViewCell *cellToZoom =(UICollectionViewCell *)[self.imagecollection cellForItemAtIndexPath:indexPath];
CGRect zoomFrameFrom = cellToZoom.frame;
[self.view addSubview:zoomImage];
zoomImage.frame = zoomFrameFrom;
zoomImage.alpha = 0.2;
[UIView animateWithDuration:0.01 animations:
 ^{
     zoomImage.frame = zoomFrameTo;
     zoomImage.alpha = 1;
    } completion:nil];
}

这里Imagecollection是我的Collectionview。

2 个答案:

答案 0 :(得分:0)

您不是使用标准方法缩放collectionView:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath    
// [self.collectionView.collectionViewLayout invalidateLayout];

在您的情况下,您使用与单元格相同的内容全屏创建了UIImageView,并将此UIImageView添加为subview以涵盖所有内容。因此,如果您将zoomImage.alpha设为0并取消隐藏收藏视图,则可获得所需内容。完成时不要忘记removeFromSuperview zoomImage。

答案 1 :(得分:0)

您可以添加点击手势识别器,甚至是覆盖与缩放图像相同区域的透明按钮。正如#kabarga所说,不要忘记删除FromSuperview以摆脱你的imageView。