Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #266 from gavrix/flowlayout_crash_fix

#255 fix and better supplementary views support in animations
  • Loading branch information...
commit f5c6dfaca3085788e5d7c685a74ab497c636e8ed 2 parents 7dce692 + 3ed0e9f
Peter Steinberger authored
51 PSTCollectionView/PSTCollectionView.m
View
@@ -1654,38 +1654,47 @@ - (void)updateWithItems:(NSArray *)items {
for (PSTCollectionViewItemKey *key in [_allVisibleViewsDict keyEnumerator]) {
PSTCollectionReusableView *view = _allVisibleViewsDict[key];
- NSInteger oldGlobalIndex = [_update[@"oldModel"] globalIndexForItemAtIndexPath:key.indexPath];
- NSArray *oldToNewIndexMap = _update[@"oldToNewIndexMap"];
- NSInteger newGlobalIndex = NSNotFound;
- if (oldGlobalIndex >= 0 && oldGlobalIndex < [oldToNewIndexMap count]) {
- newGlobalIndex = [oldToNewIndexMap[oldGlobalIndex] intValue];
- }
- NSIndexPath *newIndexPath = newGlobalIndex == NSNotFound ? nil : [_update[@"newModel"] indexPathForItemAtGlobalIndex:newGlobalIndex];
- if (newIndexPath) {
-
-
- PSTCollectionViewLayoutAttributes* startAttrs = nil;
- PSTCollectionViewLayoutAttributes* finalAttrs = nil;
-
- if (view.layoutAttributes && view.layoutAttributes.isSupplementaryView){
- startAttrs = [_layout layoutAttributesForSupplementaryViewOfKind:view.layoutAttributes.representedElementKind atIndexPath:newIndexPath];
- finalAttrs = [_layout layoutAttributesForSupplementaryViewOfKind:view.layoutAttributes.representedElementKind atIndexPath:newIndexPath];
-
+ if (key.type == PSTCollectionViewItemTypeCell) {
+ NSInteger oldGlobalIndex = [_update[@"oldModel"] globalIndexForItemAtIndexPath:key.indexPath];
+ NSArray *oldToNewIndexMap = _update[@"oldToNewIndexMap"];
+ NSInteger newGlobalIndex = NSNotFound;
+ if (oldGlobalIndex >= 0 && oldGlobalIndex < [oldToNewIndexMap count]) {
+ newGlobalIndex = [oldToNewIndexMap[oldGlobalIndex] intValue];
}
- else{
+ NSIndexPath *newIndexPath = newGlobalIndex == NSNotFound ? nil : [_update[@"newModel"] indexPathForItemAtGlobalIndex:newGlobalIndex];
+ if (newIndexPath) {
+
+
+ PSTCollectionViewLayoutAttributes* startAttrs = nil;
+ PSTCollectionViewLayoutAttributes* finalAttrs = nil;
+
startAttrs = [_layout initialLayoutAttributesForAppearingItemAtIndexPath:newIndexPath];
finalAttrs = [_layout layoutAttributesForItemAtIndexPath:newIndexPath];
-
+
+ NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{@"view":view}];
+ if (startAttrs) dic[@"previousLayoutInfos"] = startAttrs;
+ if (finalAttrs) dic[@"newLayoutInfos"] = finalAttrs;
+
+ [animations addObject:dic];
+ PSTCollectionViewItemKey* newKey = [key copy];
+ [newKey setIndexPath:newIndexPath];
+ newAllVisibleView[newKey] = view;
}
+ } else if (key.type == PSTCollectionViewItemTypeSupplementaryView) {
+ PSTCollectionViewLayoutAttributes* startAttrs = nil;
+ PSTCollectionViewLayoutAttributes* finalAttrs = nil;
+
+ startAttrs = view.layoutAttributes;
+ finalAttrs = [_layout layoutAttributesForSupplementaryViewOfKind:view.layoutAttributes.representedElementKind atIndexPath:key.indexPath];
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{@"view":view}];
if (startAttrs) dic[@"previousLayoutInfos"] = startAttrs;
if (finalAttrs) dic[@"newLayoutInfos"] = finalAttrs;
-
+
[animations addObject:dic];
PSTCollectionViewItemKey* newKey = [key copy];
- [newKey setIndexPath:newIndexPath];
newAllVisibleView[newKey] = view;
+
}
}
NSArray *allNewlyVisibleItems = [_layout layoutAttributesForElementsInRect:self.visibleBoundRects];
33 PSTCollectionView/PSTCollectionViewFlowLayout.m
View
@@ -215,22 +215,33 @@ - (PSTCollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSInd
- (PSTCollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
NSUInteger sectionIndex = indexPath.section;
+ PSTCollectionViewLayoutAttributes *layoutAttributes = nil;
+
if (sectionIndex < _data.sections.count) {
PSTGridLayoutSection *section = _data.sections[sectionIndex];
- CGRect normalizedHeaderFrame = section.headerFrame;
-
- if (!CGRectIsEmpty(normalizedHeaderFrame)) {
- normalizedHeaderFrame.origin.x += section.frame.origin.x;
- normalizedHeaderFrame.origin.y += section.frame.origin.y;
-
- PSTCollectionViewLayoutAttributes *layoutAttributes = [[[self class] layoutAttributesClass] layoutAttributesForSupplementaryViewOfKind:PSTCollectionElementKindSectionHeader withIndexPath:[NSIndexPath indexPathForItem:0 inSection:sectionIndex]];
- layoutAttributes.frame = normalizedHeaderFrame;
-
- return layoutAttributes;
+
+ CGRect normilazedFrame = CGRectZero;
+
+ if ([kind isEqualToString:PSTCollectionElementKindSectionHeader]) {
+ normilazedFrame = section.headerFrame;
+ }
+ else if ([kind isEqualToString:PSTCollectionElementKindSectionFooter]) {
+ normilazedFrame = section.footerFrame;
}
+
+ if (!CGRectIsEmpty(normilazedFrame)) {
+ normilazedFrame.origin.x += section.frame.origin.x;
+ normilazedFrame.origin.y += section.frame.origin.y;
+
+ layoutAttributes = [[[self class] layoutAttributesClass] layoutAttributesForSupplementaryViewOfKind:kind withIndexPath:[NSIndexPath indexPathForItem:0 inSection:sectionIndex]];
+ layoutAttributes.frame = normilazedFrame;
+
+ }
+
+
}
- return nil;
+ return layoutAttributes;
}
- (PSTCollectionViewLayoutAttributes *)layoutAttributesForDecorationViewWithReuseIdentifier:(NSString*)identifier atIndexPath:(NSIndexPath *)indexPath {
44 PSTCollectionView/PSTCollectionViewLayout.m
View
@@ -298,17 +298,18 @@ - (void)prepareForCollectionViewUpdates:(NSArray *)updateItems {
for (PSTCollectionReusableView *view in [[_collectionView visibleViewsDict] objectEnumerator]) {
PSTCollectionViewLayoutAttributes *attr = [view.layoutAttributes copy];
-
- PSTCollectionViewData* oldModel = update[@"oldModel"];
- NSInteger index = [oldModel globalIndexForItemAtIndexPath:[attr indexPath]];
-
- if(index != NSNotFound) {
- index = [update[@"oldToNewIndexMap"][index] intValue];
+ if (attr.isCell) {
+
+ NSInteger index = [update[@"oldModel"] globalIndexForItemAtIndexPath:[attr indexPath]];
+
if(index != NSNotFound) {
- [attr setIndexPath:[update[@"newModel"] indexPathForItemAtGlobalIndex:index]];
- _initialAnimationLayoutAttributesDict[[PSTCollectionViewItemKey collectionItemKeyForLayoutAttributes:attr]] = attr;
+ index = [update[@"oldToNewIndexMap"][index] intValue];
+ if(index != NSNotFound) {
+ [attr setIndexPath:[update[@"newModel"] indexPathForItemAtGlobalIndex:index]];
+ }
}
}
+ _initialAnimationLayoutAttributesDict[[PSTCollectionViewItemKey collectionItemKeyForLayoutAttributes:attr]] = attr;
}
PSTCollectionViewData* collectionViewData = [_collectionView collectionViewData];
@@ -316,14 +317,16 @@ - (void)prepareForCollectionViewUpdates:(NSArray *)updateItems {
CGRect bounds = [_collectionView visibleBoundRects];
for (PSTCollectionViewLayoutAttributes* attr in [collectionViewData layoutAttributesForElementsInRect:bounds]) {
- NSInteger index = [collectionViewData globalIndexForItemAtIndexPath:attr.indexPath];
-
- index = [update[@"newToOldIndexMap"][index] intValue];
- if(index != NSNotFound) {
- PSTCollectionViewLayoutAttributes* finalAttrs = [attr copy];
- [finalAttrs setIndexPath:[update[@"oldModel"] indexPathForItemAtGlobalIndex:index]];
- [finalAttrs setAlpha:0];
- _finalAnimationLayoutAttributesDict[[PSTCollectionViewItemKey collectionItemKeyForLayoutAttributes:finalAttrs]] = finalAttrs;
+ if (attr.isCell) {
+ NSInteger index = [collectionViewData globalIndexForItemAtIndexPath:attr.indexPath];
+
+ index = [update[@"newToOldIndexMap"][index] intValue];
+ if(index != NSNotFound) {
+ PSTCollectionViewLayoutAttributes* finalAttrs = [attr copy];
+ [finalAttrs setIndexPath:[update[@"oldModel"] indexPathForItemAtGlobalIndex:index]];
+ [finalAttrs setAlpha:0];
+ _finalAnimationLayoutAttributesDict[[PSTCollectionViewItemKey collectionItemKeyForLayoutAttributes:finalAttrs]] = finalAttrs;
+ }
}
}
@@ -388,7 +391,14 @@ - (PSTCollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemA
}
- (PSTCollectionViewLayoutAttributes *)initialLayoutAttributesForInsertedSupplementaryElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath {
- return nil;
+ PSTCollectionViewLayoutAttributes* attrs = _initialAnimationLayoutAttributesDict[[PSTCollectionViewItemKey collectionItemKeyForCellWithIndexPath:elementIndexPath]];
+
+ if([_insertedSectionsSet containsIndex:[elementIndexPath section]]) {
+ attrs = [attrs copy];
+ [attrs setAlpha:0];
+ }
+ return attrs;
+
}
- (PSTCollectionViewLayoutAttributes *)finalLayoutAttributesForDeletedSupplementaryElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath {
Please sign in to comment.
Something went wrong with that request. Please try again.