我如何实现一个UICollectionViewCell的动画,随着翻转和成长,可以轻松显示模态视图?
解决方法
这是我在另一个项目中使用的,它运行良好:
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
if (cell.selected) {
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
[UIView transitionWithView:cell
duration:0.2
options:UIViewAnimationoptionTransitionFlipFromLeft
animations:^{
[cell setFrame:self.selectedCellDefaultFrame];
cell.transform = self.selectedCellDefaultTransform;
}
completion:^(BOOL finished) {
self.selectedCellDefaultFrame = CGRectZero;
[collectionView reloadItemsAtIndexPaths:@[indexPath]];
}];
return NO;
}
else {
return YES;
}
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
[cell.superview bringSubviewToFront:cell];
self.selectedCellDefaultFrame = cell.frame;
self.selectedCellDefaultTransform = cell.transform;
[UIView transitionWithView:cell
duration:0.2
options:UIViewAnimationoptionTransitionFlipFromright
animations:^{
[cell setFrame:collectionView.bounds];
cell.transform = CGAffineTransformMakeRotation(0.0);
}
completion:^(BOOL finished) {}];
}
不同的东西在这里:
> bringSubviewToFront:消息调用用于防止单元格在其他单元格之后动画化
>我们使用控制器中声明的两个属性:selectedCellDefaultFrameand selectedCellDefaultTransform来保存单元格的默认状态,并在取消选择时重新初始化
>当取消选择时,我们调用UICollectionView的reloadItemsAtIndexPaths:方法来确保位置的重置完全完成
如果您有任何麻烦,请告诉我们.
祝你好运,