UICollectionView的header懸停

 

ios 9以後UICollectionViewFlowLayout中新增了sectionHeadersPinToVisibleBounds

@property (nonatomic) BOOL sectionHeadersPinToVisibleBounds API_AVAILABLE(ios(9.0));

設置

layou.sectionHeadersPinToVisibleBounds = YES;

[layout setScrollDirection:UICollectionViewScrollDirectionVertical];//上下滑動的

就可以讓UICollectionView的所有header懸停,

如果想要某個Section的Header懸停的話,就要 繼承UICollectionViewFlowLayout,重寫方法

//在layoutAttributesForSupplementaryViewOfKind方法中添加限制條件就可以設置某個Section懸停

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect

{

     NSMutableArray *superArray = [[super layoutAttributesForElementsInRect:rect] mutableCopy];

    NSMutableIndexSet *missingSections = [NSMutableIndexSet indexSet];

    for (NSUInteger idx=0; idx<[superArray count]; idx++) {

        UICollectionViewLayoutAttributes *layoutAttributes = superArray[idx];

        if (layoutAttributes.representedElementCategory == UICollectionElementCategoryCell) {

            [missingSections addIndex:layoutAttributes.indexPath.section];  // remember that we need to layout header for this section

        }

        if ([layoutAttributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {

            [superArray removeObjectAtIndex:idx];  // remove layout of header done by our super, we will do it right later

            idx--;

        }

    }

    // layout all headers needed for the rect using self code

    [missingSections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {

        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:idx];

        UICollectionViewLayoutAttributes *layoutAttributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];

        if (layoutAttributes) {

             [superArray addObject:layoutAttributes];

        }

    }];

    for (UICollectionViewLayoutAttributes *attr in self.decorationViewAttrs) {

        if (CGRectIntersectsRect(rect, attr.frame)) {

            [superArray addObject:attr];

        }

    }

    return [superArray copy];

}

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];

    //添加 indexPath.section == 3條件是爲了讓第三個Section懸停,其他的正常,如果不設置,就是所有的都懸停

    if ([kind isEqualToString:UICollectionElementKindSectionHeader] && indexPath.section == 3) {

        UICollectionView * const cv = self.collectionView;

        CGPoint const contentOffset = cv.contentOffset;

        CGPoint nextHeaderOrigin = CGPointMake(INFINITY, INFINITY);

        if (indexPath.section+1 < [cv numberOfSections]) {

            UICollectionViewLayoutAttributes *nextHeaderAttributes = [super layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:[NSIndexPath indexPathForItem:0 inSection:indexPath.section+1]];

            nextHeaderOrigin = nextHeaderAttributes.frame.origin;

        }

        CGRect frame = attributes.frame;

        if (self.scrollDirection == UICollectionViewScrollDirectionVertical) {

            frame.origin.y = MIN(MAX(contentOffset.y, frame.origin.y), nextHeaderOrigin.y - CGRectGetHeight(frame));

        }

        else { // UICollectionViewScrollDirectionHorizontal

            frame.origin.x = MIN(MAX(contentOffset.x, frame.origin.x), nextHeaderOrigin.x - CGRectGetWidth(frame));

        }

        attributes.zIndex = 1024;

        attributes.frame = frame;

    }

    return attributes;

}

- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];

    return attributes;

}

- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];

    return attributes;

}

- (BOOL) shouldInvalidateLayoutForBoundsChange:(CGRect)newBound{

    return YES;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章