1

I have custom collectionview cell (item). I have many UILabels in each item. Depending on data from server, I want to update the label appropriately. Updating the data by reloading the corresponding item works fine. Code:

 isUpdate = YES;
 [_collectionView performBatchUpdates:^{
                    [_collectionView reloadItemsAtIndexPaths:bufferUpdateRow];
                } completion:^(BOOL finished) {}];

In the cellForItemAtIndexPath method, I check for changes, and if there are any, I want to use animation block, but it seems not working.

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellID = @"CellID";

    CollectionViewCell *cell = (CollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];

// Update label content here

  if (isUpdate) {
        _label1.layer.backgroundColor = [UIColor redColor];
        [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^(void) {
                _label1.layer.backgroundColor=[UIColor clearColor].CGColor;
        } completion:nil];

        isUpdate = NO;
  }
  return cell;
}

The label is not flashing red as I want. Instead it just stay clear. If I remove the animation block, the label becomes red (which shows the updating detection works fine). Pls help

shallowThought
  • 19,212
  • 9
  • 65
  • 112
GeneCode
  • 7,545
  • 8
  • 50
  • 85

3 Answers3

2

It's probably because you are creating the animation in the cellForRowAtIndexPath method. This method is just for initialising the cell and setting up its data, not for animating it.

You'd have better chances implementing UITableViewDelegate and put the code inside:

func tableView(tableView: UITableView, willDisplayCell: UITableViewCell, forRowAtIndexPath: NSIndexPath)
Yoam Farges
  • 773
  • 4
  • 16
1

Make sure the red background is displayed before setting it clear again. One way to achieve this would be to place the animation in the completion block of another one:

[UIView animateWithDuration:0.0 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^(void) {
     self.view.layer.backgroundColor = [UIColor redColor].CGColor;
} completion:^(BOOL finished) {
    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^(void) {
        self.view.layer.backgroundColor=[UIColor clearColor].CGColor;
    } completion:nil];
}];
shallowThought
  • 19,212
  • 9
  • 65
  • 112
0

I have fallen in the same issue.but is may bad...

I am working on iOS14.

  1. in collectionViewCell you CAN call animation
  2. I missed self.layoutIfNeeded()

(in may sample I make a red label appear from right)

final func showSwipeToDeleteAnimated(){
    let W = self.frame.width
    
    UIView.animate(withDuration: 1, delay: 0.3, options: []) {
        self.redSwipeToDeleteViewViewWidthConstraint.constant = W
        self.layoutIfNeeded()
    } completion: { (_) in
        
    }
}
ingconti
  • 10,876
  • 3
  • 61
  • 48