在选择时动画显示UICollectionViewCell大小

时间:2014-03-15 23:49:21

标签: ios animation uicollectionviewcell uicollectionviewlayout caanimation

所以我有一个子类uicollectionflowlayout。我试图在选择和取消选择时为单元格大小设置动画。 选择单元格我有这个:

[CATransaction begin];
[CATransaction setCompletionBlock:^{
    BubblesCollectionViewFlowLayout *layout = (BubblesCollectionViewFlowLayout *)bubblesView.collectionViewLayout;
    [bubble.layer setCornerRadius:BIGGER_CELL_WIDTH_HEIGHT/2];
    [layout setSize:CGSizeMake(BIGGER_CELL_WIDTH_HEIGHT, BIGGER_CELL_WIDTH_HEIGHT) forItemAtIndexPath:indexPathOfSelectedCell];
    [bubble.cellLabel setFrame:CGRectMake(0.0f, BIGGER_CELL_WIDTH_HEIGHT/2-30.0f, BIGGER_CELL_WIDTH_HEIGHT, 60.0f)];
}];
/************* Cell Scale Animation *****************/
CABasicAnimation *animationScale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animationScale.fromValue = [NSNumber numberWithFloat:1.0];
animationScale.toValue = [NSNumber numberWithFloat:BIGGER_CELL_WIDTH_HEIGHT/CELL_WIDTH_HEIGHT];
[animationScale setDuration:0.3f];
[animationScale setValue:[NSNumber numberWithInt:animationTypeCellScale]
                  forKey:ANIMATION_TYPE_STRING];
[bubble.layer addAnimation:animationScale forKey:@"animationCellScale"];

/*BubblesCollectionViewFlowLayout *layout = (BubblesCollectionViewFlowLayout *)bubblesView.collectionViewLayout;
[bubble.layer setCornerRadius:BIGGER_CELL_WIDTH_HEIGHT/2];
[layout setSize:CGSizeMake(BIGGER_CELL_WIDTH_HEIGHT, BIGGER_CELL_WIDTH_HEIGHT) forItemAtIndexPath:indexPathOfSelectedCell];*/

/************** Cell label Animations ****************/
CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
[groupAnimation setDuration:0.3f];
CABasicAnimation *animationCellScale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animationCellScale.fromValue = [NSNumber numberWithFloat:1.0f];
animationCellScale.toValue = [NSNumber numberWithFloat:1.1f];
[animationCellScale setValue:[NSNumber numberWithInt:animationTypeLabelScale]
                      forKey:ANIMATION_TYPE_STRING];
[groupAnimation setAnimations:@[animationCellScale]];
[bubble.cellLabel.layer addAnimation:groupAnimation forKey:@"animationLabel"];
//[bubble.cellLabel setAlpha:0.0f];
/*[bubble.cellLabel setFrame:CGRectMake(0.0f, BIGGER_CELL_WIDTH_HEIGHT/2-30.0f, BIGGER_CELL_WIDTH_HEIGHT, 60.0f)];*/
[CATransaction commit];

这个问题是在动画期间,单元格仍然采用旧布局属性,因此单元格仍然是正常大小。动画很好地发生但是当它完成时它会快速恢复到正常大小,然后再进入CATransaction完成块。因此,我看到动画后从正常到大尺寸的突然变化。这是复制的,但是以相反的顺序取消选择。任何有用的提示将不胜感激

1 个答案:

答案 0 :(得分:0)

与任何动画一样,您需要在动画开始之前将对象设置为其最终值。这样,当动画结束时,它的最终值与您已经巧妙地给出该对象的 real 值相匹配。

换句话说,事先做一下你当前正在做的事务完成块。