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

Cin*_*lia 2 objective-c ios uicollectionview

我正在UIViewController使用UICollectionView标准网格布局进行安排allowsMultipleSelection = NO.当用户选择一个单元格(通过handleTap选择UITapGestureRecognizer)时,我想执行以下一项或全部操作:

(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]);
    }
}
Run Code Online (Sandbox Code Playgroud)

我也尝试过这里建议的布局无效,但无论出于何种原因,它在我选择的单元格周围创建了一个巨大的缓冲区,扰乱了它周围的所有其他单元格(导致它们移动到新位置),然而内容(imageView) )在我放大的自定义单元格内部不会放大.即它内部的imageView保持相同的大小,即使我专门改变了imageView.frameimageView.image的大小:UICollectionView:选择时动画单元格大小更改

以下是更多示例代码,即使它在选定的单元格上执行也不起作用:

-(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];

}
Run Code Online (Sandbox Code Playgroud)

Nav*_*Dev 9

您无需调用invalidateLayout来执行这些动画.

只需didSelectItemAtIndexPath通过调用获取单元格的实例即可

 MyCollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)

并为细胞设置动画.

这是我用来缩放单元格的代码片段.

 [self animateZoomforCell:cell];
Run Code Online (Sandbox Code Playgroud)

// ---方法实现 - //

-(void)animateZoomforCell:(MyCollectionViewCell*)zoomCell
    {
         [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{

            zoomCell.transform = CGAffineTransformMakeScale(1.6,1.6);
        } completion:^(BOOL finished){
         }];
    }
Run Code Online (Sandbox Code Playgroud)