Uicollectionview header hover, inherit Uicollectionviewflowlayout, override related methods
////StickyHeaderLayout.h//Wombat////Created by Todd Laney on 1/9/13.//Copyright (c) toddla. All rights reserved.////Modified fromhttp://blog.radi.ws/post/32905838158/sticky-headers-for-uicollectionview-usingthanks!// #import<UIKit/UIKit.h>@interfaceStickyheaderlayout:uicollectionviewflowlayout@end
////STICKYHEADERFLOWLAYOUT.M//Wombat////Created by Todd Laney on 1/9/13.//Copyright (c) toddla. All rights reserved.////Modified fromhttp://blog.radi.ws/post/32905838158/sticky-headers-for-uicollectionview-usingthanks!// #import "StickyHeaderFlowLayout.h" @implementationStickyheaderflowlayout-(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 'll do it right lateridx--; } } //layout All headers needed-the RECT using self code[Missingsections enumerateindexesusingblock:^ (Nsuinteger idx, BOOL *stop) {Nsindexpath*indexpath = [Nsindexpath indexpathforitem:0Insection:idx]; Uicollectionviewlayoutattributes*layoutattributes =[self Layoutattributesforsupplementaryviewofkind:uicollectionelementkindsectionheader atIndexPath:indexPath] ; [Answer addobject:layoutattributes]; }]; returnanswer;} -(Uicollectionviewlayoutattributes *) Layoutattributesforsupplementaryviewofkind: (NSString *) kind AtIndexPath: ( Nsindexpath *) Indexpath {uicollectionviewlayoutattributes*attributes =[Super Layoutattributesforsupplementaryviewofkind:kind Atindexpath:indexpath]; if([Kind Isequaltostring:uicollectionelementkindsectionheader]) {Uicollectionview*ConstCV =Self.collectionview; CgpointConstContentoffset =Cv.contentoffset; Cgpoint Nextheaderorigin=Cgpointmake (INFINITY, INFINITY); if(indexpath.section+1<[CV numberofsections]) {Uicollectionviewlayoutattributes*nextheaderattributes = [Super Layoutattributesforsupplementaryviewofkind:kind Atindexpath:[nsindexpath Indexpathforitem:0insection: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{//Uicollectionviewscrolldirectionhorizontalframe.origin.x = MIN (MAX (Contentoffset.x, frame.origin.x), nextheaderorigin.x-Cgrectgetwidth (frame)); } Attributes.zindex=1024x768; Attributes.frame=frame; } returnattributes;} -(Uicollectionviewlayoutattributes *) Initiallayoutattributesforappearingsupplementaryelementofkind: (NSString *) Kind Atindexpath: (Nsindexpath *) Indexpath {uicollectionviewlayoutattributes*attributes =[self layoutattributesforsupplementaryviewofkind:kind atindexpath:indexpath]; returnattributes;}-(Uicollectionviewlayoutattributes *) Finallayoutattributesfordisappearingsupplementaryelementofkind: (NSString *) Kind Atindexpath: (Nsindexpath *) Indexpath {uicollectionviewlayoutattributes*attributes =[self layoutattributesforsupplementaryviewofkind:kind atindexpath:indexpath]; returnattributes;} -(BOOL) Shouldinvalidatelayoutforboundschange: (CGRect) Newbound {returnYES;} @end
Source: https://gist.github.com/evadne/4544569
Uicollectionview's header hover