Ios 突出显示或放大所选单元格?

Ios 突出显示或放大所选单元格?,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我在一个UIViewController中工作,它的UICollectionView以标准网格布局排列,并且allowsMultipleSelection=NO。当用户选择单元格(通过handleTapUITapGestureRecognitizer选择)时,我希望执行以下一项或全部操作: (1) 用不同的颜色突出显示/着色单元格--更改其alpha和背景色 (2) 在不改变周围任何其他细胞的情况下,稍微放大细胞 (3) 围绕修改后的单元格边框绘制边框 我目前正在使用以下函数,尽管单元格被正确地

我在一个
UIViewController
中工作,它的
UICollectionView
以标准网格布局排列,并且
allowsMultipleSelection=NO
。当用户选择单元格(通过handleTap
UITapGestureRecognitizer
选择)时,我希望执行以下一项或全部操作:

(1) 用不同的颜色突出显示/着色单元格--更改其alpha和背景色

(2) 在不改变周围任何其他细胞的情况下,稍微放大细胞

(3) 围绕修改后的单元格边框绘制边框

我目前正在使用以下函数,尽管单元格被正确地划分为选定单元格,并存储在局部变量
selectedCell
中,但该函数从未被调用

-(CGSize) collectionView:(UICollectionView*)collectionView layout:(UICollectionViewFlowLayout*)layout sizeForItemAtIndexPath: (NSIndexPath*)indexPath
{
    if ([indexPath isEqual:selectedCellIndexPath]) 
 {

        return CGSizeMake([MyCollectionViewCell width] + 4, [MyCollectionViewCell height] + 4);
    } else {
        return CGSizeMake([MyCollectionViewCell width], [MyCollectionViewCell height]);
    }
}
我也尝试过按这里的建议使布局无效,但无论出于什么原因,它都会在我选择的单元格周围创建一个巨大的缓冲区,干扰它周围的所有其他单元格(导致它们移动到一个新位置),但我放大的自定义单元格内的内容(imageView)不会放大。也就是说,它内部的imageView保持相同的大小,即使我专门转换了imageView.frame和imageView.image的大小:

下面是更多示例代码,这些代码即使在选定的单元格上执行也无法工作:

-(void) collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath
{
    NSLog(@"Cell Selected!");
    MyCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StorageCell" forIndexPath:indexPath];
    MyImageView* selectedCellView = cell.imgView;

    MyCollectionViewFlowLayout* layout = (MyCollectionViewFlowLayout*)self.collection.collectionViewLayout;

    layout.currentCellPath = indexPath;
    layout.currentCellCenter = selectedCellView.center;
    layout.currentCellScale = 1.25;

    [self.collection.collectionViewLayout invalidateLayout];

    // Animate

    [UIView transitionWithView:cell duration:0.1f options: UIViewAnimationOptionCurveLinear animations:^(void)
     {
         CGRect frame = cell.frame;
         frame.size = CGSizeMake(60.0, 100.0);
         cell.frame = frame;

         selectedCellView.imgView.image = [UIImage imageNamed:@"wood2.png"];
         [self.collection reloadItemsAtIndexPaths:@[indexPath]];

         NSLog(@"Cell Frame animated to: %f,%f", cell.frame.size.width, cell.frame.size.height);

         frame = selectedCellView.frame;
         frame.size.width = 1.25*frame.size.width;
         frame.size.height = 1.25*frame.size.height;
         selectedCellView.frame = frame;

         frame = selectedCellView.imgView.frame;
         frame.size.width = 1.25*frame.size.width;
         frame.size.height = 1.25*frame.size.height;
         selectedCellView.imgView.frame = frame;

         UICollectionViewLayoutAttributes* attrib = [collection layoutAttributesForItemAtIndexPath:indexPath];
         attrib.transform3D = CATransform3DMakeScale(2, 2, 1.0);
         selectedCellView.imgView.frame.transform = CGAffineTransformScale(selectedCellView.imgView.transform, 1.1, 1.1);

     }
                    completion:nil];

}

您不需要调用invalidateLayout来执行这些动画

只需调用

 MyCollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
并为细胞设置动画

下面是我用来缩放单元格的代码片段

 [self animateZoomforCell:cell];
//---方法实现--//


谢谢我是否需要在取消选择方法中执行任何操作来撤消放大?是的,只需实现一个类似的方法,并将转换设置为
cGraffinetTransformMakeScale(1,1)
,即可恢复。
-(void)animateZoomforCell:(MyCollectionViewCell*)zoomCell
    {
         [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{

            zoomCell.transform = CGAffineTransformMakeScale(1.6,1.6);
        } completion:^(BOOL finished){
         }];
    }