在Tap上动画UICollectionViewCell

时间:2012-10-31 08:38:03

标签: ios6 core-animation uicollectionview uicollectionviewcell

当用户点按某个单元格时,我想在UICollectionViewCell上启动一些动画。我的想法是在didSelectItemAtIndexPath中选择相应的单元格并触发动画。但是,这不起作用:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// animate the cell user tapped on
ProductCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ProductReuseID" forIndexPath:indexPath];

[UIView animateWithDuration:5.0
                      delay:0
                    options:(UIViewAnimationOptionAllowUserInteraction)
                 animations:^{
                     NSLog(@"animation start");
                     [cell.layer setBackgroundColor:[UIColor colorWithRed: 180.0/255.0 green: 238.0/255.0 blue:180.0/255.0 alpha: 1.0].CGColor];
                 }
                 completion:^(BOOL finished){
                     NSLog(@"animation end");
                     [cell.layer setBackgroundColor:[UIColor whiteColor].CGColor];
                 }
 ];
}

实际上,动画同时开始和结束(尽管animateWithDuration设置为5)。下一次尝试是跳过动画并简单地设置一个不同的边框样式:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// animate the cell user tapped on
ProductCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ProductReuseID" forIndexPath:indexPath];

[cell.layer setBorderWidth:5.0f];
}

然而,这并没有改变任何东西(可能是因为我必须手动重绘单元格?)。

您有任何想法如何在用户点击UICollectionViewCell时为其设置动画吗?

亲切的问候,     基督教

2 个答案:

答案 0 :(得分:31)

看起来你得到了错误的Cell。发送dequeueReusableCellWithReuseIdentifier:forIndexPath:消息在第二个参数中的indexPath视图中获取正在使用的单元格,但是将先前使用但可重用的单元格出列;如果没有可重用的单元可用,则创建一个新单元。见下面的参考文献1。

更换:

ProductCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ProductReuseID" forIndexPath:indexPath];

使用:

ProductCollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];

在上面的代码中,应该为您提供合适的单元格。

这是你的第一个例子,改写了。

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath(NSIndexPath *)indexPath
{
    // animate the cell user tapped on
    UICollectionViewCell  *cell = [collectionView cellForItemAtIndexPath:indexPath];    
    [UIView animateWithDuration:5.0
            delay:0
            options:(UIViewAnimationOptionAllowUserInteraction)
                animations:^{
                    NSLog(@"animation start");
                    [cell setBackgroundColor:[UIColor colorWithRed: 180.0/255.0 green: 238.0/255.0 blue:180.0/255.0 alpha: 1.0]];
                }
                completion:^(BOOL finished){
                    NSLog(@"animation end");
                    [cell setBackgroundColor:[UIColor whiteColor]];
                }
    ];
}

参考文献:

答案 1 :(得分:2)

您可以通过按照代码选择/点击具有自定义动画持续时间的UICollectionViewCell来自定义动画。因此,您无需更改其背景颜色。

使用以下选项 - UIViewAnimationOption

  • UIViewAnimationOptionCurveEaseIn
  • UIViewAnimationOptionCurveEaseOut
  • UIViewAnimationOptionAllowUserInteraction

    UICollectionViewDelegate - didSelectItemAtIndexPath方法

    UICollectionViewCell *uviCollectionCell =  [collectionView cellForItemAtIndexPath:indexPath];
    
    [UIView animateWithDuration:0.4 delay:0 options:(UIViewAnimationOptionCurveEaseIn) animations:^{
            NSLog(@"animation start");
            CALayer *layer = uviCollectionCell.layer;
            CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
            rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, 15.0f * M_PI / 180.0f, 1.0f, 0.0f, 0.0f);
            layer.transform = rotationAndPerspectiveTransform;
    } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.3 delay:0 options:(UIViewAnimationOptionCurveEaseOut) animations:^{
                NSLog(@"animation end");
                CALayer *layer = uviCollectionCell.layer;
                CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
                rotationAndPerspectiveTransform.m24 = 0;
                rotationAndPerspectiveTransform =CATransform3DRotate(rotationAndPerspectiveTransform, 0.0f * M_PI / 180.0f, 1.0f, 0.0f, 0.0f);
                layer.transform = rotationAndPerspectiveTransform;
            } completion:nil];
        }
    ];