ios - 如何只修改从 collectionView 中选择的项目的大小和位置?

标签 ios objective-c uicollectionview

我有一个 Collection View ,当我选择一个项目时,我想放大该图像并添加一个框架。我这样做了,但其余的图像也在移动。这就是我所做的:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    if ([selectedIndexPath isEqual:indexPath]) {
       selectedIndexPath = nil;
        [self.player stop];

        CollectionViewCell *cellOne =  [collectionView cellForItemAtIndexPath:indexPath];
        cellOne.inner.hidden=YES;


        recipeImageView.frame=CGRectMake(recipeImageView.frame.origin.x, recipeImageView.frame.origin.y, 100, 100);
        recipeImageView.layer.borderColor = [[UIColor blackColor] CGColor];

    }
    else {
        // select new cell
        celulaSelectata=indexPath;
        UICollectionViewCell  *cell = [collectionView cellForItemAtIndexPath:indexPath];
        recipeImageView = (UIImageView *)[cell viewWithTag:100];

        CollectionViewCell *cellOne =  [collectionView cellForItemAtIndexPath:indexPath];

        recipeImageView.frame=CGRectMake(recipeImageView.frame.origin.x, recipeImageView.frame.origin.y, 120, 120);
        cellOne.inner.frame=recipeImageView.frame;

如何仅修改所选项目的大小和位置?

最佳答案

selectedIndexPath创建 UICollectionViewLayoutAttributes对于选定的单元格并调用 invalidateItemsAtIndexPaths:(NSArray *)indexPaths更新所选 UICollectionViewLayoutAttributes 的单元格

子类化您的 UICollectionViewLayout并在您的UICollectionViewLayout中添加功能并使任何 indexPath 无效更新

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes *attribute = [super layoutAttributesForItemAtIndexPath:indexPath];
    if ([indexPath isEqual:selectedIndexPath]) {
        //set attributes
        //attribute.frame = //setframe;
    }
    return attribute;
}

关于ios - 如何只修改从 collectionView 中选择的项目的大小和位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28961171/

相关文章:

ios - 获取图像时出错

ios - 如何在新的 UIViewController 中打开我在 UIWebView 中的 mp3 链接?

ios - 使用 StageWebView 加载 Youtube 视频时裁剪

swift - 如何在 UICollectionView 单元格中获取 UIImageView 的帧大小

ios - Collection View 树表示

ios - 是否有可能读取 iOS 设备存储信息

ios - 解析 dSYM (Mach-O) 文件。字节顺序问题

ios - 自定义 UITableViewCell - 我设置了背景图像,但如何设置边框颜色?

objective-c - 如何在基于 View 的 NSTableView 中绑定(bind) NSButton 的点击 Action

iphone - 不滚动的 UITableView header