UIcollectionview支持封面流程

时间:2014-10-13 10:23:23

标签: ios xcode ios7 uicollectionview coverflow

嗨朋友可以在水平布局中在uicollectionview中添加封面流效果。如果可能,请告诉我如何实现效果。

2 个答案:

答案 0 :(得分:2)

是的,您可以实现自定义UICollectionViewFlowLayout。

创建一个继承UICollectionViewFlowLayout

的自定义类
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    return YES; }

- (UICollectionViewScrollDirection)scrollDirection {
    return UICollectionViewScrollDirectionVertical; }

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {

    UICollectionView *collectionView = [self collectionView];
    UIEdgeInsets insets = [collectionView contentInset];
    CGPoint offset = [collectionView contentOffset];
    CGFloat minY = -insets.top;

    NSArray *attributes = [super layoutAttributesForElementsInRect:rect];

    // minY is Point where we implement this cover flow.
    if (offset.y < minY) {  

        // Figure out how much we've pulled down 
        CGFloat deltaY = fabsf(offset.y - minY);

        for (UICollectionViewLayoutAttributes *attrs in attributes) {

            // Locate the header attributes
            NSString *kind = [attrs representedElementKind];
            if (kind == UICollectionElementKindSectionHeader) {

                // This is header's height and y based on how much the user has scrolled down.
                CGSize headerSize = [self headerReferenceSize];
                CGRect headerRect = [attrs frame];
                headerRect.size.height = MAX(minY, headerSize.height + deltaY);
                headerRect.origin.y = headerRect.origin.y - deltaY;
                [attrs setFrame:headerRect];
                break;
            }
        }
    }
    return attributes; }

现在在您的类中分配UICollectionView

CustomCoverFlowHeaderCollectionViewLayout *flow;  flow = [[CustomCoverFlowHeaderCollectionViewLayout alloc] init]; [stretchyLayout setHeaderReferenceSize:CGSizeMake(320.0, 160.0)];   // Set our custom layout [collectionView setCollectionViewLayout: flow];  [collectionView setAlwaysBounceVertical:YES];    [collectionView registerClass:[UICollectionReusableView class]    forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
          withReuseIdentifier:@"myCoverCollectionView"];

你差不多完成了

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView 
       viewForSupplementaryElementOfKind:(NSString *)kind 
                             atIndexPath:(NSIndexPath *)indexPath {
if (!header) {

    header = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                withReuseIdentifier:@"myCoverCollectionView" forIndexPath:indexPath];
    CGRect bounds;
    bounds = [header bounds];

    UIImageView *imageView;
    imageView = [[UIImageView alloc] initWithFrame:bounds];
    [imageView setImage:[UIImage imageNamed:@"background"]];
    [imageView setContentMode:UIViewContentModeScaleAspectFill];
    [imageView setClipsToBounds:YES];
    [imageView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    [header addSubview:imageView];
}
return header; }

答案 1 :(得分:1)

Here是一个基于UICollectionView的好项目。