2014-10-13 3 views
1

Приветствую, что можно добавить эффект обтекания в виде uicollection в горизонтальном макете. Если возможно, любезно скажите мне, как реализовать эффект.UIcollection view cover cover flow

ответ

1

Да, это возможно, вам необходимо реализовать свой пользовательский 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; } 
+0

Убедитесь, что вы используете блоки кода для форматирования блоков без кодов. Просто выделите код и нажмите CMD-K – Fogmeister

1

Here хороший проект, основанный на UICollectionView для вас.