UICollectionView的header悬停,可设置某个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;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在HTML中,可以使用CSS和JavaScript来实现鼠标悬停展开的效果。具体的做法是通过CSS定义展开内容的初始样式和鼠标悬停时的样式,然后通过JavaScript来控制样式的变化。 以下是一个简单的示例代码,实现鼠标悬停展开的效果: ```html <div class="container"> <div class="header">这是一个标题</div> <div class="content">这是展开的内容</div> </div> <style> .header { background-color: #eee; padding: 10px; cursor: pointer; } .content { display: none; padding: 10px; } .container:hover .content { display: block; } </style> <script> var container = document.querySelector('.container'); var content = document.querySelector('.content'); container.addEventListener('mouseenter', function() { content.style.display = 'block'; }); container.addEventListener('mouseleave', function() { content.style.display = 'none'; }); </script> ``` 这段代码中,我们首先定义了一个包含标题和内容的容器,其中标题使用了样式`.header`,内容使用了样式`.content`。在CSS中,我们使用了`.content`的`display`属性来控制内容的初始状态为隐藏,使用了`.container:hover .content`的选择器来定义鼠标悬停时的样式为显示。 在JavaScript中,我们使用`document.querySelector`方法获取容器和内容的元素,使用`addEventListener`方法来添加鼠标悬停和移出的事件监听器,在事件处理函数中控制样式的变化,从而实现鼠标悬停展开的效果。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值