【问题标题】:UIcollection view support cover flowUIcollectionview 支持封面流
【发布时间】:2014-12-07 21:14:21
【问题描述】:

你好朋友,是否可以在水平布局的uicollection视图中添加封面流效果。如果可以的话,请告诉我如何实现效果。

【问题讨论】:

    标签: ios xcode ios7 uicollectionview coverflow


    【解决方案1】:

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

    【讨论】:

      【解决方案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; }
      

      【讨论】:

      • 确保使用代码块来格式化代码而不是引用块。只需突出显示代码并按 CMD-K
      猜你喜欢
      • 1970-01-01
      • 2013-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-28
      • 1970-01-01
      • 2016-05-27
      相关资源
      最近更新 更多