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

San*_*osh 10 iphone ios uicollectionview uicollectionviewcell ios7

我正在使用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;
}
Run Code Online (Sandbox Code Playgroud)

这里,old_path包含最后选择的图像的索引.

Aus*_*tin 1

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

[UIView transitionFromView:cell1.selectedBackgroundView toView:cell1.contentView duration:0.5 options:UIViewAnimationOptionCurveLinear |UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
Run Code Online (Sandbox Code Playgroud)

UICollectionViewCellcontentViewselectedBackgroundView不应该像这样搞乱,因为单元格管理它们的布局。此代码将完全删除内容视图并将其替换为背景视图,当选择时,背景视图预计位于内容视图的后面,并且当未选择时从视图层次结构中删除。

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


您的调整大小代码中也可能存在翻转图像的内容,但如果没有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];
Run Code Online (Sandbox Code Playgroud)

其他一些观察结果:

集合视图单元格被重用,这意味着您的实现collectionView:cellForItemAtIndexPath:最终可能会将一大堆图像视图添加到已多次出列的单元格中。更好的解决方案是子类化UICollectionViewCell并在其方法中添加自定义视图init

该代码old_path==indexPath实际上并不测试两个索引路径之间的相等性,只是两个变量在内存中具有相同的地址。[oldPath isEqual:indexPath]代替使用。