I have a collection view whose layout is managed by a UICollectionViewFlowLayout instance. I have a long press gesture recognizer attached to the collection view whose job is to detect if a long press occured within the bounds of one of the cells in the collection view and if so, delete that cell after changing the cell’s transform animatedly.
This is the relevant part of the action performed by the gesture recognizer:
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
[UIView animateWithDuration:2.0 animations:^{
cell.layer.transform = CATransform3DMakeScale(1.0, 0.0, 1.0); // an example animation - squish the cell vertically
} completion:^(BOOL fin) {
//[cell.layer removeAllAnimations];
[self.model.numb3rs removeObjectAtIndex:[indexPath row]]; // update model
[self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]]; // delete item
}];
The problem is that the deletion method seems to perform its own property change/animation on the cell which doesn’t gel well with my animation. For instance, in the effect I’m trying to achieve here, once the cell is completely squished vertically, I’d like it not to show itself in the view again, but the deleteItemsAtIndexPaths: call causes the cell’s “ghost” to show up and then fade out as the method performs its own transform and opacity animation.
Am I approaching it completely wrong? Possibly the collection view architecture offers a better way to do what I want, rather than explicitly animating the cell and then deleting it like I’m trying?
I’m hoping I might be able to get a quick answer here rather than have to dig into the documentation too deep.
I asked this question on the Apple Developer Forums, and was informed of the
method which is part of the UICollectionViewLayout class.
Anyway, by subclassing UICollectionViewFlowLayout and overriding this method, I was able to modify the attributes of the cell I was deleting – in particular, the transform.
One thing to note is that this method gets called not only for the cell being deleted but also for cells being shifted from their position during the relayout. But it’s simple enough to write code to keep track of the cell being deleted and only modified its attributes.