UICollectionViewLayout更新zIndex wh

时间:2015-07-02 17:31:09

标签: ios objective-c

我正在使用Objective C并定位iOS 7。

我为UICollectionViewLayout实现了UICollectionView,我希望单元格在彼此之上堆叠,但我希望将顶部的单元格缩小。像第一张照片一样。

Top Collection View

然而,当我向下滚动时,单元格的比例没有更新:

Middle Collection View

我正在尝试像Android最近的应用程序菜单那样做:

Android Recents Menu

以下是我的代码的相关部分:

- (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 < 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;
}

0 个答案:

没有答案