Ответ 1
Вот что я использовал в другом проекте, и он работал хорошо:
- (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:
используется для предотвращения анимации ячейки за другими ячейками. - Мы используем два свойства, объявленные в контроллере:
selectedCellDefaultFrame
иselectedCellDefaultTransform
, чтобы сохранить состояние ячейки по умолчанию и повторно инициализировать ее при отмене выбора - При отмене выбора мы вызываем метод
reloadItemsAtIndexPaths:
UICollectionView
, чтобы убедиться, что reset позиции полностью завершена
Сообщите мне, есть ли у вас проблемы с этим.
Удачи,