ASCollectionLayout to exclude content inset on scrollable directions from viewport size (#562)

This commit is contained in:
Huy Nguyen 2017-09-11 21:32:17 +01:00 committed by Garrett Moon
parent fcee108af5
commit 7c7a4acf0e
2 changed files with 50 additions and 13 deletions

View File

@ -10,7 +10,7 @@
- Negate iOS 11 automatic estimated table row heights. [Christian Selig](https://github.com/christianselig) [#485](https://github.com/TextureGroup/Texture/pull/485)
- Add content inset and offset bridging properties to ASTableNode and ASCollectionNode. Deprecate related properties and methods in ASTableView and ASCollectionView [Huy Nguyen](https://github.com/nguyenhuy) [#460](https://github.com/TextureGroup/Texture/pull/460) [#560](https://github.com/TextureGroup/Texture/pull/560)
- Remove re-entrant access to self.view when applying initial pending state. [Adlai Holler](https://github.com/Adlai-Holler) [#510](https://github.com/TextureGroup/Texture/pull/510)
- Small improvements in ASCollectionLayout [Huy Nguyen](https://github.com/nguyenhuy) [#509](https://github.com/TextureGroup/Texture/pull/509) [#513](https://github.com/TextureGroup/Texture/pull/513)
- Small improvements in ASCollectionLayout [Huy Nguyen](https://github.com/nguyenhuy) [#509](https://github.com/TextureGroup/Texture/pull/509) [#513](https://github.com/TextureGroup/Texture/pull/513) [#562]((https://github.com/TextureGroup/Texture/pull/562)
- Fix retain cycle between ASImageNode and PINAnimatedImage [Phil Larson](https://github.com/plarson) [#520](https://github.com/TextureGroup/Texture/pull/520)
- Change the API for disabling logging from a compiler flag to a runtime C function ASDisableLogging(). [Adlai Holler](https://github.com/Adlai-Holler) [#528](https://github.com/TextureGroup/Texture/pull/528)
- Table and collection views to consider content inset when calculating (default) element size range [Huy Nguyen](https://github.com/nguyenhuy) [#525](https://github.com/TextureGroup/Texture/pull/525)

View File

@ -69,18 +69,35 @@ static const ASScrollDirection kASStaticScrollDirection = (ASScrollDirectionRigh
- (ASCollectionLayoutContext *)layoutContextWithElements:(ASElementMap *)elements
{
ASDisplayNodeAssertMainThread();
CGSize viewportSize = [self _viewportSize];
CGPoint contentOffset = _collectionNode.contentOffset;
Class<ASCollectionLayoutDelegate> layoutDelegateClass = [_layoutDelegate class];
ASCollectionLayoutCache *layoutCache = _layoutCache;
ASCollectionNode *collectionNode = _collectionNode;
if (collectionNode == nil) {
return [[ASCollectionLayoutContext alloc] initWithViewportSize:CGSizeZero
initialContentOffset:CGPointZero
scrollableDirections:ASScrollDirectionNone
elements:[[ASElementMap alloc] init]
layoutDelegateClass:layoutDelegateClass
layoutCache:layoutCache
additionalInfo:nil];
}
ASScrollDirection scrollableDirections = [_layoutDelegate scrollableDirections];
CGSize viewportSize = [ASCollectionLayout _viewportSizeForCollectionNode:collectionNode scrollableDirections:scrollableDirections];
CGPoint contentOffset = collectionNode.contentOffset;
id additionalInfo = nil;
if (_layoutDelegateFlags.implementsAdditionalInfoForLayoutWithElements) {
additionalInfo = [_layoutDelegate additionalInfoForLayoutWithElements:elements];
}
return [[ASCollectionLayoutContext alloc] initWithViewportSize:viewportSize
initialContentOffset:contentOffset
scrollableDirections:[_layoutDelegate scrollableDirections]
scrollableDirections:scrollableDirections
elements:elements
layoutDelegateClass:[_layoutDelegate class]
layoutCache:_layoutCache
layoutDelegateClass:layoutDelegateClass
layoutCache:layoutCache
additionalInfo:additionalInfo];
}
@ -208,21 +225,41 @@ static const ASScrollDirection kASStaticScrollDirection = (ASScrollDirectionRigh
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return (! CGSizeEqualToSize([self _viewportSize], newBounds.size));
return (! CGSizeEqualToSize([ASCollectionLayout _boundsForCollectionNode:_collectionNode], newBounds.size));
}
#pragma mark - Private methods
- (CGSize)_viewportSize
+ (CGSize)_boundsForCollectionNode:(nonnull ASCollectionNode *)collectionNode
{
ASCollectionNode *collectionNode = _collectionNode;
if (collectionNode != nil && !collectionNode.isNodeLoaded) {
if (collectionNode == nil) {
return CGSizeZero;
}
if (!collectionNode.isNodeLoaded) {
// TODO consider calculatedSize as well
return collectionNode.threadSafeBounds.size;
} else {
ASDisplayNodeAssertMainThread();
return self.collectionView.bounds.size;
}
ASDisplayNodeAssertMainThread();
return collectionNode.view.bounds.size;
}
+ (CGSize)_viewportSizeForCollectionNode:(nonnull ASCollectionNode *)collectionNode scrollableDirections:(ASScrollDirection)scrollableDirections
{
if (collectionNode == nil) {
return CGSizeZero;
}
CGSize result = [ASCollectionLayout _boundsForCollectionNode:collectionNode];
// TODO: Consider using adjustedContentInset on iOS 11 and later, to include the safe area of the scroll view
UIEdgeInsets contentInset = collectionNode.contentInset;
if (ASScrollDirectionContainsHorizontalDirection(scrollableDirections)) {
result.height -= (contentInset.top + contentInset.bottom);
} else {
result.width -= (contentInset.left + contentInset.right);
}
return result;
}
/**