title: UICollectionViewLayout updating zIndex wh
body: <p>I am using Objective C and targeting iOS 7.</p>
  
  <p>I have implemented a <code>UICollectionViewLayout</code> for <code>UICollectionView</code> where I want the cells to stack on top of each other, but I want the cells to the top to be scaled down. Like the first picture. </p>
  
  <p><img src="https://i.stack.imgur.com/p7NzB.png" alt="Top Collection View"></p>
  
  <p>However, when i scroll down the scale of the cells is not being updated:</p>
  
  <p><img src="https://i.stack.imgur.com/6tEfs.png" alt="Middle Collection View"></p>
  
  <p>I am attempting to do something like Android's recent applications menu:</p>
  
  <p><img src="https://i.stack.imgur.com/srK0K.jpg" alt="Android Recents Menu"></p>
  
  <p>Here are relevant parts of my code:</p>
  
  <pre><code>- (void)prepareLayout {
      [self collectionViewContentSize];
  
      CGFloat itemReveal = self.topReveal;
      CGSize itemSizee = self.itemSize;
  
      if (CGSizeEqualToSize(itemSizee, CGSizeZero)) {
          itemSizee = CGSizeMake(CGRectGetWidth(self.collectionView.frame) - self.layoutMargin.left - self.layoutMargin.right,
                                 CGRectGetHeight(self.collectionView.frame) - self.layoutMargin.top - self.layoutMargin.bottom - self.collectionView.contentInset.top - self.collectionView.contentInset.bottom - 200);
      }
  
      self.overwriteContentOffset = NO;
  
      NSMutableDictionary *layoutAttributes = [NSMutableDictionary dictionary];
      NSInteger itemCount = [self.collectionView numberOfItemsInSection:0];
  
      for (NSInteger item = 0; item &lt; itemCount; item++) {
          NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:0];
          UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
  
          // Cards overlap each other
          // via z depth
          //
          attributes.zIndex = item;
  
          // The moving item is hidden
          //
          attributes.hidden = [attributes.indexPath isEqual:self.movingIndexPath];
  
          // By default all items are layed
          // out evenly with each revealing
          // only top part ...
          //
          attributes.frame = CGRectMake(self.layoutMargin.left, self.layoutMargin.top + itemReveal * item, itemSizee.width, itemSizee.height);
  
          NSLog(@"Frame : %@",NSStringFromCGRect(attributes.frame));
          layoutAttributes[indexPath] = attributes;
      }
  
      self.layoutAttributes = layoutAttributes;
  }
  
  - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
      NSMutableArray *layoutAttributes = [NSMutableArray array];
      __block NSInteger zIndex = 0;
  
      [self.layoutAttributes enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *indexPath, UICollectionViewLayoutAttributes *attributes, BOOL *stop) {
  
          if (CGRectIntersectsRect(rect, attributes.frame)) {
              attributes.zIndex = zIndex++;
  
              CGAffineTransform transform = CGAffineTransformIdentity;
              CGFloat scale = [self scaleToIndex:attributes.zIndex];
              transform = CGAffineTransformScale(transform, scale, scale);
              attributes.transform = transform;
  
              [layoutAttributes addObject:attributes];
          }
      }];
  
      return layoutAttributes;
  }
  
  - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
      return self.layoutAttributes[indexPath];
  }
  
  - (CGFloat)scaleToIndex:(NSInteger)zIndex{
      return -1/(zIndex + 1.6)+1;
  }
  </code></pre>