向下滚动时,UICollectionView中的图像会自动翻转?

时间:2014-01-31 11:38:26

标签: ios iphone ios7 uicollectionview uicollectionviewcell

我正在使用UICollectionView,我正在显示手机照片库中的所有图像。

当我点击任何图像时,图像会被翻转,并显示有关图像的一些信息。

当用户再次点击同一图像时,图像再次翻转并显示原始图像。

问题在于,每当我向下滚动UICollectionView时,最后选择的图像会自动翻转,并显示有关图像的信息。

如何解决这个问题。

以下是一些代码:

- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    {
        UICollectionViewCell *cell1 = [collectionView cellForItemAtIndexPath:indexPath];
        if(old_path!=NULL){
        UICollectionViewCell *cell2 = [collectionView cellForItemAtIndexPath:old_path];
        [UIView transitionFromView:cell2.selectedBackgroundView toView:cell2.contentView duration:0.5 options:UIViewAnimationOptionCurveLinear |UIViewAnimationOptionTransitionFlipFromLeft completion:nil];

    }
if(old_path==indexPath&&flag)
{
  [cell1 setSelected:NO];
  [UIView transitionFromView:cell1.selectedBackgroundView toView:cell1.contentView duration:0.5 options:UIViewAnimationOptionCurveLinear |UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
    flag=FALSE;
}
else{
    [UIView transitionFromView:cell1.contentView toView:cell1.selectedBackgroundView duration:0.5 options:UIViewAnimationOptionCurveLinear |UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
    flag=TRUE;
}
old_path=indexPath;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    ALAsset *asset = assets[indexPath.row];
    NSLog(@"Description : %@",[asset description]);
    UIImage *img=[self imageWithImage:[UIImage imageWithCGImage:[asset thumbnail]] convertToSize:CGSizeMake(150, 150)];
    UIView *contents = [[UIView alloc]initWithFrame:cell.bounds];
    contents.backgroundColor = [UIColor colorWithPatternImage:img];

    [cell.contentView addSubview:contents];

    UIView *backgroundView = [[UIView alloc]initWithFrame:cell.bounds];
    backgroundView.backgroundColor = [UIColor yellowColor];
    UIButton *del=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    del.frame= CGRectMake(backgroundView.frame.origin.x+20,     backgroundView.frame.origin.y+20, 100, 40);
    [del setTitle:@"Delete" forState:UIControlStateNormal];
    [del addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:del];
    UIButton *cancel=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    cancel.frame= CGRectMake(backgroundView.frame.origin.x+20, backgroundView.frame.origin.y+80, 100, 45);
    [cancel setTitle:@"Cancel" forState:UIControlStateNormal];
    [cancel addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:cancel];

    cell.selectedBackgroundView = backgroundView;
    [cell bringSubviewToFront:cell.selectedBackgroundView];
    return cell;
}

此处,old_path包含上次选择图像的索引。

2 个答案:

答案 0 :(得分:1)

主要问题可能在于UIView转换方法:

[UIView transitionFromView:cell1.selectedBackgroundView toView:cell1.contentView duration:0.5 options:UIViewAnimationOptionCurveLinear |UIViewAnimationOptionTransitionFlipFromLeft completion:nil];

UICollectionViewCell的{​​{1}}和contentView不应该像这样混淆,因为单元格管理它们的布局。此代码将完全删除内容视图,并将其替换为背景视图,背景视图应在选定时位于内容视图后面,并在未选中时从视图层次结构中删除。

完成你正在做的事情(显示/隐藏图像以响应点击)的正确方法是在图像视图本身和内容视图上的另一个子视图之间进行转换。


您的大小调整代码中也可能存在翻转图像的内容,但如果没有selectedBackgroundView的代码,则很难说。摆脱这种方法可能会更有效,并做这样的事情:

imageWithImage:convertToSize:

其他一些观察结果:

集合视图单元格重用,这意味着您的实现UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.bounds]; imageView.contentsMode = UIViewContentModeScaleAspectFill; imageView.clipsToBounds = YES; imageView.image = [UIImage imageWithCGImage:[asset thumbnail]]; [cell.contentsView addSubview:imageView]; 最终可能会将一大堆图像视图添加到已多次出列的单元格中。更好的解决方案是将collectionView:cellForItemAtIndexPath:子类化,并在其UICollectionViewCell方法中添加自定义视图。

代码init实际上并不测试两个索引路径之间的相等性,只是两个变量在内存中具有相同的地址。请改用old_path==indexPath

答案 1 :(得分:0)

如果没有明确的子类化和处理,您无法在后台视图上执行自定义动画。因为如果selectedBackgroundViewselectedBackgroundView不同,集合视图会自动将backgroundView置于顶部。添加Apple文档以供参考。

// The background view is a subview behind all other views.
// If selectedBackgroundView is different than backgroundView, it will be placed above the background view and animated in on selection. 

https://developer.apple.com/library/iOs/documentation/UIKit/Reference/UICollectionViewCell_class/Reference/Reference.html