ios – UICollectionView中的图像在向下滚动时会自动翻转?

前端之家收集整理的这篇文章主要介绍了ios – UICollectionView中的图像在向下滚动时会自动翻转?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用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,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包含最后选择的图像的索引.

解决方法

主要的问题可能在于UIView转换方法
[UIView transitionFromView:cell1.selectedBackgroundView toView:cell1.contentView duration:0.5 options:UIViewAnimationOptionCurveLinear |UIViewAnimationOptionTransitionFlipFromLeft completion:nil];

UICollectionViewCell的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实际上并不测试两个索引路径之间的相等性,只要这两个变量在内存中具有相同的地址.改用[oldPath isEqual:indexPath].

猜你在找的iOS相关文章