我正在使用 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 包含最后选择的图像的索引。
最佳答案
主要问题可能出在 UIView 转换方法上:
[UIView transitionFromView:cell1.selectedBackgroundView toView:cell1.contentView duration:0.5 options:UIViewAnimationOptionCurveLinear |UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
UICollectionViewCell
的 contentView
和 selectedBackgroundView
不应该被这样搞乱,因为单元格管理它们的布局。此代码将完全删除内容 View 并将其替换为背景 View ,该背景 View 在被选中时应位于内容 View 之后,而在未被选中时会从 View 层次结构中移除。
完成您正在做的事情(显示/隐藏图像以响应点击)的正确方法是在 ImageView 本身和内容 View 的另一个 subview 之间执行转换。
在调整大小的代码中也可能有一些东西会翻转图像,但是如果没有 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];
一些其他观察结果:
Collection View 单元格被重用,这意味着您的实现 collectionView:cellForItemAtIndexPath:
最终可能会将一整堆 ImageView 添加到一个已出队的单元格中次。更好的解决方案是继承 UICollectionViewCell
并在其 init
方法中添加自定义 View 。
代码 old_path==indexPath
实际上并没有测试两个索引路径之间的相等性,只是两个变量在内存中的地址是否相同。请改用 [oldPath isEqual:indexPath]
。
关于ios - 向下滚动时,UICollectionView 中的图像会自动翻转吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21479174/