Skip to content

Commit

Permalink
added set for headers and footers so they appear and disappear correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
odyth committed Feb 14, 2014
1 parent faff71c commit 4d52b8d
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions UICollectionView-Spring-Demo/TLSpringFlowLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ @interface TLSpringFlowLayout ()

// Needed for tiling
@property (nonatomic, strong) NSMutableSet *visibleIndexPathsSet;
@property (nonatomic, strong) NSMutableSet *visibleHeaderAndFooterSet;
@property (nonatomic, assign) CGFloat latestDelta;
@property (nonatomic, assign) UIInterfaceOrientation interfaceOrientation;

Expand All @@ -41,6 +42,7 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder {
- (void)setup {
_dynamicAnimator = [[UIDynamicAnimator alloc] initWithCollectionViewLayout:self];
_visibleIndexPathsSet = [NSMutableSet set];
_visibleHeaderAndFooterSet = [[NSMutableSet alloc] init];
}

- (void)prepareLayout {
Expand All @@ -62,20 +64,20 @@ - (void)prepareLayout {

// Step 1: Remove any behaviours that are no longer visible.
NSArray *noLongerVisibleBehaviours = [self.dynamicAnimator.behaviors filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UIAttachmentBehavior *behaviour, NSDictionary *bindings) {
BOOL currentlyVisible = [itemsIndexPathsInVisibleRectSet member:[[[behaviour items] firstObject] indexPath]] != nil;
return !currentlyVisible;
return [itemsIndexPathsInVisibleRectSet containsObject:[[[behaviour items] firstObject] indexPath]] == NO;
}]];

[noLongerVisibleBehaviours enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
[self.dynamicAnimator removeBehavior:obj];
[self.visibleIndexPathsSet removeObject:[[[obj items] firstObject] indexPath]];
[self.visibleHeaderAndFooterSet removeObject:[[[obj items] firstObject] indexPath]];
}];

// Step 2: Add any newly visible behaviours.
// A "newly visible" item is one that is in the itemsInVisibleRect(Set|Array) but not in the visibleIndexPathsSet
NSArray *newlyVisibleItems = [itemsInVisibleRectArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UICollectionViewLayoutAttributes *item, NSDictionary *bindings) {
BOOL currentlyVisible = [self.visibleIndexPathsSet member:item.indexPath] != nil;
return !currentlyVisible;
return (item.representedElementCategory == UICollectionElementCategoryCell ?
[self.visibleIndexPathsSet containsObject:item.indexPath] : [self.visibleHeaderAndFooterSet containsObject:item.indexPath]) == NO;
}]];

CGPoint touchLocation = [self.collectionView.panGestureRecognizer locationInView:self.collectionView];
Expand Down Expand Up @@ -117,7 +119,14 @@ - (void)prepareLayout {
}

[self.dynamicAnimator addBehavior:springBehaviour];
[self.visibleIndexPathsSet addObject:item.indexPath];
if(item.representedElementCategory == UICollectionElementCategoryCell)
{
[self.visibleIndexPathsSet addObject:item.indexPath];
}
else
{
[self.visibleHeaderAndFooterSet addObject:item.indexPath];
}
}];
}

Expand Down

0 comments on commit 4d52b8d

Please sign in to comment.