iosobjective-cuicollectionviewuianimation

UIView animation block for UILabel in UICollectionViewCell not working


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


Solution

  • 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];
    }];