ios - UIcollectionview支持覆盖流

标签 ios xcode ios7 uicollectionview coverflow

嗨 friend 们,是否可以在水平布局的 Collection View 中添加覆盖流效果。如果可以的话请告诉我如何实现该效果。

最佳答案

是的,这是可能的,您需要实现自定义的 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; }

关于ios - UIcollectionview支持覆盖流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26337736/

相关文章:

ios - 在Swift中按下UITextfield上的返回按钮时,如何添加空格?

ios - 在 iOS 11 上隐藏时状态栏背景消失

ios - xcode如何在数据库中的 Collection View 单元格中显示图像

ios - iOS NSZombiesEnabled =是

ios6 - AVAudioPlayer 直接到扬声器/耳机而不是听筒 iOS 7

ios7 - 使用自主SingleAppModePermittedAppIDs 和 UIAccessibilityRequestGuidedAccessSession 与 Meraki 作为 MDM 进入单应用模式

ios - 类型 'String' 不符合协议(protocol) 'NilLiteralConvertiblle'

ios - 关于 xcode Assets 目录和应用程序大小

ios - 将 UILabel 结果保存到 CoreData 中

ios - 如何在 Xcode 模拟器中关闭 iPhone X 的电源?