mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-09-06 04:32:06 +00:00
80 lines
2.2 KiB
Objective-C
80 lines
2.2 KiB
Objective-C
//
|
|
// ASPagerFlowLayout.m
|
|
// AsyncDisplayKit
|
|
//
|
|
// Created by Levi McCallum on 2/12/16.
|
|
// Copyright © 2016 Facebook. All rights reserved.
|
|
//
|
|
|
|
#import "ASPagerFlowLayout.h"
|
|
|
|
@interface ASPagerFlowLayout ()
|
|
|
|
@property (strong, nonatomic) NSIndexPath *currentIndexPath;
|
|
|
|
@end
|
|
|
|
@implementation ASPagerFlowLayout
|
|
|
|
- (void)invalidateLayout
|
|
{
|
|
self.currentIndexPath = [self _indexPathForVisiblyCenteredItem];
|
|
[super invalidateLayout];
|
|
}
|
|
|
|
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset
|
|
{
|
|
if (self.currentIndexPath) {
|
|
return [self _targetContentOffsetForItemAtIndexPath:self.currentIndexPath
|
|
proposedContentOffset:proposedContentOffset];
|
|
}
|
|
|
|
return [super targetContentOffsetForProposedContentOffset:proposedContentOffset];
|
|
}
|
|
|
|
- (CGPoint)_targetContentOffsetForItemAtIndexPath:(NSIndexPath *)indexPath proposedContentOffset:(CGPoint)proposedContentOffset
|
|
{
|
|
UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:indexPath];
|
|
CGFloat xOffset = (self.collectionView.bounds.size.width - attributes.frame.size.width) / 2;
|
|
return CGPointMake(attributes.frame.origin.x - xOffset, proposedContentOffset.y);
|
|
}
|
|
|
|
- (NSIndexPath *)_indexPathForVisiblyCenteredItem
|
|
{
|
|
if ([self _dataSourceIsEmpty]) {
|
|
return nil;
|
|
}
|
|
|
|
CGRect visibleRect = [self _visibleRect];
|
|
CGFloat visibleXCenter = CGRectGetMidX(visibleRect);
|
|
NSArray<UICollectionViewLayoutAttributes *> *layoutAttributes = [self layoutAttributesForElementsInRect:visibleRect];
|
|
for (UICollectionViewLayoutAttributes *attributes in layoutAttributes) {
|
|
if ([attributes representedElementCategory] == UICollectionElementCategoryCell && attributes.center.x == visibleXCenter) {
|
|
return attributes.indexPath;
|
|
}
|
|
}
|
|
return nil;
|
|
}
|
|
|
|
- (BOOL)_dataSourceIsEmpty
|
|
{
|
|
if ([self.collectionView numberOfSections]) {
|
|
if ([self.collectionView numberOfItemsInSection:0] == 0) {
|
|
return YES;
|
|
}
|
|
} else {
|
|
return YES;
|
|
}
|
|
return NO;
|
|
}
|
|
|
|
- (CGRect)_visibleRect
|
|
{
|
|
CGRect visibleRect;
|
|
visibleRect.origin = self.collectionView.contentOffset;
|
|
visibleRect.size = self.collectionView.bounds.size;
|
|
return visibleRect;
|
|
}
|
|
|
|
@end
|