From db6e997a5cf5665e05943b70cbe5ba506df5d5af Mon Sep 17 00:00:00 2001 From: James Tang Date: Thu, 4 Jun 2015 12:34:55 +0800 Subject: [PATCH 1/2] Fixes #68 Updating transform3D to fix visual bug that cause by performBatchUpdates: --- Classes/CSStickyHeaderFlowLayout.m | 62 ++++++++++++++++ Classes/CSStickyHeaderFlowLayoutAttributes.m | 10 +++ .../CSLockedHeaderViewController.m | 70 ++++++++++++++++-- .../bbb.imageset/Contents.json | 21 ++++++ .../Images.xcassets/bbb.imageset/bbb.png | Bin 0 -> 3955 bytes 5 files changed, 155 insertions(+), 8 deletions(-) create mode 100644 Project/CSStickyHeaderFlowLayoutDemo/Images.xcassets/bbb.imageset/Contents.json create mode 100644 Project/CSStickyHeaderFlowLayoutDemo/Images.xcassets/bbb.imageset/bbb.png diff --git a/Classes/CSStickyHeaderFlowLayout.m b/Classes/CSStickyHeaderFlowLayout.m index 8a98b6c..cc96a2a 100644 --- a/Classes/CSStickyHeaderFlowLayout.m +++ b/Classes/CSStickyHeaderFlowLayout.m @@ -11,6 +11,12 @@ NSString *const CSStickyHeaderParallaxHeader = @"CSStickyHeaderParallexHeader"; +@interface CSStickyHeaderFlowLayout (Debug) + +- (void)debugLayoutAttributes:(NSArray *)layoutAttributes; + +@end + @implementation CSStickyHeaderFlowLayout - (void)prepareLayout { @@ -160,6 +166,9 @@ - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect }]; } + // For debugging purpose +// [self debugLayoutAttributes:allItems]; + return allItems; } @@ -229,3 +238,56 @@ - (void)updateHeaderAttributes:(UICollectionViewLayoutAttributes *)attributes la } @end + +#pragma mark - Debugging + +@implementation CSStickyHeaderFlowLayoutAttributes (Debug) + +- (NSString *)description { + NSString *indexPathString = [NSString stringWithFormat:@"{%ld, %ld}", (long)self.indexPath.section, (long)self.indexPath.item]; + + NSString *desc = [NSString stringWithFormat:@" indexPath: %@ zIndex: %ld valid: %@ kind: %@", self, indexPathString, (long)self.zIndex, [self isValid] ? @"YES" : @"NO", self.representedElementKind ?: @"cell"]; + + return desc; +} + +- (BOOL)isValid { + switch (self.representedElementCategory) { + case UICollectionElementCategoryCell: + if (self.zIndex != 1) { + return NO; + } + return YES; + case UICollectionElementCategorySupplementaryView: + if ([self.representedElementKind isEqualToString:CSStickyHeaderParallaxHeader]) { + return YES; + } else if (self.zIndex < 1024) { + return NO; + } + return YES; + default: + return YES; + } +} + +@end + + +@implementation CSStickyHeaderFlowLayout (Debug) + +- (void)debugLayoutAttributes:(NSArray *)layoutAttributes { + __block BOOL hasInvalid = NO; + [layoutAttributes enumerateObjectsUsingBlock:^(CSStickyHeaderFlowLayoutAttributes *attr, NSUInteger idx, BOOL *stop) { + hasInvalid = ![attr isValid]; + if (hasInvalid) { + *stop = YES; + } + }]; + + if (hasInvalid) { + NSLog(@"CSStickyHeaderFlowLayout: %@", layoutAttributes); + } +} + +@end + diff --git a/Classes/CSStickyHeaderFlowLayoutAttributes.m b/Classes/CSStickyHeaderFlowLayoutAttributes.m index 0876d36..ca7777c 100644 --- a/Classes/CSStickyHeaderFlowLayoutAttributes.m +++ b/Classes/CSStickyHeaderFlowLayoutAttributes.m @@ -16,4 +16,14 @@ - (id)copyWithZone:(NSZone *)zone { return copy; } +- (void)setZIndex:(NSInteger)zIndex { + [super setZIndex:zIndex]; + + // Fixes: Section header go behind cell when insert via performBatchUpdates #68 + // https://github.com/jamztang/CSStickyHeaderFlowLayout/issues/68#issuecomment-108678022 + // Reference: UICollectionView setLayout:animated: not preserving zIndex + // http://stackoverflow.com/questions/12659301/uicollectionview-setlayoutanimated-not-preserving-zindex + self.transform3D = CATransform3DMakeTranslation(0, 0, zIndex); +} + @end diff --git a/Project/CSStickyHeaderFlowLayoutDemo/CSLockedHeaderViewController.m b/Project/CSStickyHeaderFlowLayoutDemo/CSLockedHeaderViewController.m index b4b8c2e..631bef1 100644 --- a/Project/CSStickyHeaderFlowLayoutDemo/CSLockedHeaderViewController.m +++ b/Project/CSStickyHeaderFlowLayoutDemo/CSLockedHeaderViewController.m @@ -23,14 +23,7 @@ @implementation CSLockedHeaderViewController - (id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { - self.sections = @[ - @{@"Twitter":@"http://twitter.com"}, - @{@"Facebook":@"http://facebook.com"}, - @{@"Tumblr":@"http://tumblr.com"}, - @{@"Pinterest":@"http://pinterest.com"}, - @{@"Instagram":@"http://instagram.com"}, - @{@"Github":@"http://github.com"}, - ]; + self.sections = @[]; self.headerNib = [UINib nibWithNibName:@"CSSearchBarHeader" bundle:nil]; } @@ -50,6 +43,67 @@ - (void)viewDidLoad forSupplementaryViewOfKind:CSStickyHeaderParallaxHeader withReuseIdentifier:@"header"]; + UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(add:)]; + self.navigationItem.rightBarButtonItem = add; + + [self add:nil]; + +} + +- (void)add:(id)sender +{ +// NSArray *new = @[ +// @{@"Twitter":@"http://twitter.com"}, +// @{@"Facebook":@"http://facebook.com"}, +// @{@"Tumblr":@"http://tumblr.com"}, +// @{@"Pinterest":@"http://pinterest.com"}, +// @{@"Instagram":@"http://instagram.com"}, +// @{@"Github":@"http://github.com"}, +// @{@"Twitter":@"http://twitter.com"}, +// @{@"Facebook":@"http://facebook.com"}, +// @{@"Tumblr":@"http://tumblr.com"}, +// @{@"Pinterest":@"http://pinterest.com"}, +// @{@"Instagram":@"http://instagram.com"}, +// @{@"Github":@"http://github.com"}, +// ]; +// self.sections = [self.sections arrayByAddingObjectsFromArray:new]; +// [self.collectionView reloadData]; + + [self.collectionView performBatchUpdates:^{ + + NSArray *new = @[ + @{@"Twitter":@"http://twitter.com"}, + @{@"Facebook":@"http://facebook.com"}, + @{@"Tumblr":@"http://tumblr.com"}, + @{@"Pinterest":@"http://pinterest.com"}, + @{@"Instagram":@"http://instagram.com"}, + @{@"Github":@"http://github.com"}, + @{@"Twitter":@"http://twitter.com"}, + @{@"Facebook":@"http://facebook.com"}, + @{@"Tumblr":@"http://tumblr.com"}, + @{@"Pinterest":@"http://pinterest.com"}, + @{@"Instagram":@"http://instagram.com"}, + @{@"Github":@"http://github.com"}, + ]; + self.sections = [self.sections arrayByAddingObjectsFromArray:new]; + + NSMutableIndexSet *set = [NSMutableIndexSet indexSet]; + NSMutableArray *indexPaths = [NSMutableArray array]; + + int startIndex = 0; + + [new enumerateObjectsUsingBlock:^(NSDictionary *item, NSUInteger idx, BOOL *stop) { + NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:startIndex + idx]; + [indexPaths addObject:indexPath]; + + [set addIndex:startIndex + idx]; + + }]; + + [self.collectionView insertSections:set]; + [self.collectionView insertItemsAtIndexPaths:indexPaths]; + } completion:nil]; + } - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { diff --git a/Project/CSStickyHeaderFlowLayoutDemo/Images.xcassets/bbb.imageset/Contents.json b/Project/CSStickyHeaderFlowLayoutDemo/Images.xcassets/bbb.imageset/Contents.json new file mode 100644 index 0000000..e5c19ab --- /dev/null +++ b/Project/CSStickyHeaderFlowLayoutDemo/Images.xcassets/bbb.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x", + "filename" : "bbb.png" + }, + { + "idiom" : "universal", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/Project/CSStickyHeaderFlowLayoutDemo/Images.xcassets/bbb.imageset/bbb.png b/Project/CSStickyHeaderFlowLayoutDemo/Images.xcassets/bbb.imageset/bbb.png new file mode 100644 index 0000000000000000000000000000000000000000..33bbad4f7f4b6680df1fc7c61c25bc61774c7d64 GIT binary patch literal 3955 zcmeHKOK1~87@o8uwGR=z*n+TZrD!$#NRzc$8f)B5Y|t$wjWk8YZg(fm((c3EsY&W3 zc&K{wqz4gumg32qLJOWedJ}IRyonyX6nb$cn^#NIi$$Sn_#wOV{jd4|pZR8=PEB6# z>mKL^0Q99Mq_^?rk<3_V8AL=;YOB$ziQ zR$Mr}GMUj<7PY8Ok6)q2iXwJkLZnhfbIJ0=VvMf%74b7!W@)OTf)-=6pHxA`Y8=l~g23?Mh#>Hns1V18SPqL2W{|JTsr1+hlZD_KY_1}$%}=0S#@<{Az%Y>Ofu)p>gv#b}(VzXj8& zyS2P(nQ+0_qUx|b!;vl36cuHERnx3%dngSX`R?i1%ak0*rlDsqyBcm>Zjgj>M9GCJ zvfYerFV(6zmA8@YEg^$ILAJ#|<8eQWqV$>GON?<9@p z1HxyM2BEYccEqIB>jiGTPZ~RJ82&)~Z|r5@r*#pOdun&|t;aL?e*XE*$=8XqolnST Na4In=zr8m1=nwXNk&OTV literal 0 HcmV?d00001 From d8b5cf40d24598394d3c0032e4c00be45406ec8a Mon Sep 17 00:00:00 2001 From: James Tang Date: Thu, 4 Jun 2015 12:41:40 +0800 Subject: [PATCH 2/2] v0.2.6 --- CSStickyHeaderFlowLayout.podspec | 2 +- README.md | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CSStickyHeaderFlowLayout.podspec b/CSStickyHeaderFlowLayout.podspec index 307d848..f0c5ae8 100644 --- a/CSStickyHeaderFlowLayout.podspec +++ b/CSStickyHeaderFlowLayout.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "CSStickyHeaderFlowLayout" - s.version = "0.2.5" + s.version = "0.2.6" s.summary = "Parallax and Sticky header done right using UICollectionViewLayout" s.description = <<-DESC UICollectionView are flexible and you can use supplementary views to diff --git a/README.md b/README.md index 4e79416..9a8b3a5 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,8 @@ If you think this worths something, tip me a cup of coffee! (p.s. was trying out ## Updates +- 0.2.6: Fixing that section header being covered by cell after perform batch update + - 0.2.5: Fixing a crash when quickly popping back to a view controller using the parallax header - 0.2.4: Possibly fix for a crash when parallaxHeaderReferenceSize is changed