0

I working with UICollectionView, and I need add cell dynamically. all cells has blured background view.

When I add item using [self.collectionView insertItemsAtIndexPaths:indexPaths]; I have a trouble with animation cell appear. I see cells content before blur effect has applied to cell's background view.

- (SomeCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PKMainMenuCVCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    [self addBlurLayerToCell:cell];
    cell.textLabel.text = @"SomeText";

    return cell;
}

I have addBlurLayerToCell with dispatch_async, but has no effect.

-(void)addBlurLayerToCell: (SomeCollectionViewCell*) cell
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];//];
        UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        blurEffectView.frame = cell.bounds;
        blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [cell insertSubview:blurEffectView atIndex:0];
    });
}
Peter
  • 55
  • 1
  • 6
  • why are you invoking main queue. if remove that code what will happen? And try by moving code from cellForItemAtIndexPath to willDisplayCell delegate – Shebin Koshy Mar 18 '18 at 12:42
  • @ShebinKoshy same result. – Peter Mar 18 '18 at 13:19
  • Blur add again and again since its in cellForItemAtIndexPath, thats not good. So, assign tag to blurEffectView. and before adding blur, check [cell viewWithTag:] and handle it using this technique. If your problem still exist, try adding blur in awakeFromNib. – Shebin Koshy Mar 18 '18 at 14:02
  • @ShebinKoshy I adding blur in cell subclass in `awakeFromNib`, but still has same result. First, I see content of the cell label and background, it appear with animation alpha channel, and then blur effect has apply. And, I set cell's background color to `clearColor`, so background color that appear first, is set by blur effect (for example `UIBlurEffectStyleLight` has white color with some alpha transparency and `UIBlurEffectStyleDark` has black color with some alpha transparency). – Peter Mar 18 '18 at 14:24
  • @ShebinKoshy I have different result, if I using `[self.collectionView reloadData];` all works, much better. Why does it matter? – Peter Mar 18 '18 at 14:35
  • where did you call it? – Shebin Koshy Mar 18 '18 at 14:36
  • @ShebinKoshy instead `[self.collectionView insertItemsAtIndexPaths:indexPaths];` – Peter Mar 18 '18 at 14:38
  • Glad to hear that its working, Don't know the reason dear friend. :) – Shebin Koshy Mar 18 '18 at 14:53

0 Answers0