zoukankan      html  css  js  c++  java
  • UICollectionView的header悬停

    UICollectionView的header悬停,继承UICollectionViewFlowLayout,重写相关方法

    //
    //  StickyHeaderLayout.h
    //  Wombat
    //
    //  Created by Todd Laney on 1/9/13.
    //  Copyright (c) 2013 ToddLa. All rights reserved.
    //
    //  Modified from http://blog.radi.ws/post/32905838158/sticky-headers-for-uicollectionview-using THANKS!
    //
     
    #import <UIKit/UIKit.h>
     
    @interface StickyHeaderLayout : UICollectionViewFlowLayout
     
    @end
    //
    //  StickyHeaderFlowLayout.m
    //  Wombat
    //
    //  Created by Todd Laney on 1/9/13.
    //  Copyright (c) 2013 ToddLa. All rights reserved.
    //
    //  Modified from http://blog.radi.ws/post/32905838158/sticky-headers-for-uicollectionview-using THANKS!
    //
     
    #import "StickyHeaderFlowLayout.h"
     
    @implementation StickyHeaderFlowLayout
     
    - (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
     
        NSMutableArray *answer = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
        
        NSMutableIndexSet *missingSections = [NSMutableIndexSet indexSet];
        for (NSUInteger idx=0; idx<[answer count]; idx++) {
            UICollectionViewLayoutAttributes *layoutAttributes = answer[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]) {
                [answer 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];
            [answer addObject:layoutAttributes];
        }];
        
        return answer;
    }
     
    - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
        UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            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;
    }
     
    @end

    来源:https://gist.github.com/evadne/4544569

  • 相关阅读:
    第三方支付集成
    文件并发(日志处理)--队列--Redis+Log4Net
    ReportingServies——SQLServer报表开发综合实例
    C#开发可以可视化操作的windows服务
    4、ASP.NET MVC入门到精通——NHibernate构建一个ASP.NET MVC应用程序
    Lucene.net站内搜索—6、站内搜索第二版
    Lucene.net站内搜索—5、搜索引擎第一版实现
    Lucene.net站内搜索—4、搜索引擎第一版技术储备(简单介绍Log4Net、生产者消费者模式)
    谈谈爱情——祭奠那逝去的青春
    Lucene.net站内搜索—3、最简单搜索引擎代码
  • 原文地址:https://www.cnblogs.com/benbenzhu/p/4389786.html
Copyright © 2011-2022 走看看