mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-24 07:05:35 +00:00
[ASCollectionView] Finish support for interoperability with base-class UICollectionViewCells.
This also supports supplementary nodes. It builds off of Adlai's .interop flag but makes necessary improvements for all of the delegate methods to work in practice with heterogenous cell types.
This commit is contained in:
@@ -22,5 +22,6 @@
|
||||
@interface ImageCellNode : ASCellNode
|
||||
|
||||
- (instancetype)initWithImage:(UIImage *)image;
|
||||
@property (nonatomic, strong) UIImage *image;
|
||||
|
||||
@end
|
||||
|
||||
@@ -39,4 +39,14 @@
|
||||
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsZero child:_imageNode];
|
||||
}
|
||||
|
||||
- (void)setImage:(UIImage *)image
|
||||
{
|
||||
_imageNode.image = image;
|
||||
}
|
||||
|
||||
- (UIImage *)image
|
||||
{
|
||||
return _imageNode.image;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
//
|
||||
// ImageCollectionViewCell.h
|
||||
// Sample
|
||||
//
|
||||
// Created by Hannah Troisi on 1/28/17.
|
||||
// Copyright © 2017 Facebook. All rights reserved.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface ImageCollectionViewCell : UICollectionViewCell
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,46 @@
|
||||
//
|
||||
// ImageCollectionViewCell.m
|
||||
// Sample
|
||||
//
|
||||
// Created by Hannah Troisi on 1/28/17.
|
||||
// Copyright © 2017 Facebook. All rights reserved.
|
||||
//
|
||||
|
||||
#import "ImageCollectionViewCell.h"
|
||||
|
||||
@implementation ImageCollectionViewCell
|
||||
{
|
||||
UILabel *_title;
|
||||
UILabel *_description;
|
||||
}
|
||||
|
||||
- (id)initWithFrame:(CGRect)aRect
|
||||
{
|
||||
self = [super initWithFrame:aRect];
|
||||
if (self) {
|
||||
_title = [[UILabel alloc] init];
|
||||
_title.text = @"UICollectionViewCell";
|
||||
[self.contentView addSubview:_title];
|
||||
|
||||
_description = [[UILabel alloc] init];
|
||||
_description.text = @"description for cell";
|
||||
[self.contentView addSubview:_description];
|
||||
|
||||
self.contentView.backgroundColor = [UIColor orangeColor];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)layoutSubviews
|
||||
{
|
||||
[super layoutSubviews];
|
||||
|
||||
[_title sizeToFit];
|
||||
[_description sizeToFit];
|
||||
|
||||
CGRect frame = _title.frame;
|
||||
frame.origin.y = _title.frame.size.height;
|
||||
_description.frame = frame;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -28,6 +28,9 @@
|
||||
@property (assign, nonatomic) UIEdgeInsets interItemSpacing;
|
||||
@property (assign, nonatomic) CGFloat headerHeight;
|
||||
|
||||
- (CGSize)itemSizeAtIndexPath:(NSIndexPath *)indexPath;
|
||||
- (CGSize)headerSizeForSection:(NSInteger)section;
|
||||
|
||||
@end
|
||||
|
||||
@protocol MosaicCollectionViewLayoutDelegate <ASCollectionDelegate>
|
||||
@@ -35,7 +38,3 @@
|
||||
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(MosaicCollectionViewLayout *)layout originalItemSizeAtIndexPath:(NSIndexPath *)indexPath;
|
||||
|
||||
@end
|
||||
|
||||
@interface MosaicCollectionViewLayoutInspector : NSObject <ASCollectionViewLayoutInspecting>
|
||||
|
||||
@end
|
||||
|
||||
@@ -54,7 +54,7 @@
|
||||
top += _sectionInset.top;
|
||||
|
||||
if (_headerHeight > 0) {
|
||||
CGSize headerSize = [self _headerSizeForSection:section];
|
||||
CGSize headerSize = [self headerSizeForSection:section];
|
||||
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes
|
||||
layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader
|
||||
withIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];
|
||||
@@ -75,7 +75,7 @@
|
||||
NSUInteger columnIndex = [self _shortestColumnIndexInSection:section];
|
||||
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:idx inSection:section];
|
||||
|
||||
CGSize itemSize = [self _itemSizeAtIndexPath:indexPath];
|
||||
CGSize itemSize = [self itemSizeAtIndexPath:indexPath];
|
||||
CGFloat xOffset = _sectionInset.left + (columnWidth + _columnSpacing) * columnIndex;
|
||||
CGFloat yOffset = [_columnHeights[section][columnIndex] floatValue];
|
||||
|
||||
@@ -146,7 +146,7 @@
|
||||
return ([self _widthForSection:section] - ((_numberOfColumns - 1) * _columnSpacing)) / _numberOfColumns;
|
||||
}
|
||||
|
||||
- (CGSize)_itemSizeAtIndexPath:(NSIndexPath *)indexPath
|
||||
- (CGSize)itemSizeAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
CGSize size = CGSizeMake([self _columnWidthForSection:indexPath.section], 0);
|
||||
CGSize originalSize = [[self _delegate] collectionView:self.collectionView layout:self originalItemSizeAtIndexPath:indexPath];
|
||||
@@ -156,7 +156,7 @@
|
||||
return size;
|
||||
}
|
||||
|
||||
- (CGSize)_headerSizeForSection:(NSUInteger)section
|
||||
- (CGSize)headerSizeForSection:(NSInteger)section
|
||||
{
|
||||
return CGSizeMake([self _widthForSection:section], _headerHeight);
|
||||
}
|
||||
@@ -199,36 +199,3 @@
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation MosaicCollectionViewLayoutInspector
|
||||
|
||||
- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
MosaicCollectionViewLayout *layout = (MosaicCollectionViewLayout *)[collectionView collectionViewLayout];
|
||||
return ASSizeRangeMake(CGSizeZero, [layout _itemSizeAtIndexPath:indexPath]);
|
||||
}
|
||||
|
||||
- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
MosaicCollectionViewLayout *layout = (MosaicCollectionViewLayout *)[collectionView collectionViewLayout];
|
||||
return ASSizeRangeMake(CGSizeZero, [layout _headerSizeForSection:indexPath.section]);
|
||||
}
|
||||
|
||||
- (ASScrollDirection)scrollableDirections
|
||||
{
|
||||
return ASScrollDirectionVerticalDirections;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asks the inspector for the number of supplementary views for the given kind in the specified section.
|
||||
*/
|
||||
- (NSUInteger)collectionView:(ASCollectionView *)collectionView supplementaryNodesOfKind:(NSString *)kind inSection:(NSUInteger)section
|
||||
{
|
||||
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -20,14 +20,17 @@
|
||||
#import <AsyncDisplayKit/AsyncDisplayKit.h>
|
||||
#import "MosaicCollectionViewLayout.h"
|
||||
#import "ImageCellNode.h"
|
||||
#import "ImageCollectionViewCell.h"
|
||||
|
||||
// This option demonstrates that raw UIKit cells can still be used alongside native ASCellNodes.
|
||||
static BOOL kShowUICollectionViewCells = YES;
|
||||
static NSString *kReuseIdentifier = @"ImageCollectionViewCell";
|
||||
static NSUInteger kNumberOfImages = 14;
|
||||
|
||||
@interface ViewController () <ASCollectionDataSource, ASCollectionDelegate>
|
||||
@interface ViewController () <ASCollectionDataSourceInterop, ASCollectionDelegate, ASCollectionViewLayoutInspecting>
|
||||
{
|
||||
NSMutableArray *_sections;
|
||||
ASCollectionNode *_collectionNode;
|
||||
MosaicCollectionViewLayoutInspector *_layoutInspector;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -35,7 +38,7 @@ static NSUInteger kNumberOfImages = 14;
|
||||
@implementation ViewController
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark UIViewController.
|
||||
#pragma mark UIViewController
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
@@ -48,8 +51,6 @@ static NSUInteger kNumberOfImages = 14;
|
||||
_collectionNode.delegate = self;
|
||||
_collectionNode.backgroundColor = [UIColor whiteColor];
|
||||
|
||||
_layoutInspector = [[MosaicCollectionViewLayoutInspector alloc] init];
|
||||
|
||||
if (!(self = [super initWithNode:_collectionNode]))
|
||||
return nil;
|
||||
|
||||
@@ -73,7 +74,8 @@ static NSUInteger kNumberOfImages = 14;
|
||||
{
|
||||
[super viewDidLoad];
|
||||
|
||||
_collectionNode.view.layoutInspector = _layoutInspector;
|
||||
_collectionNode.view.layoutInspector = self;
|
||||
[_collectionNode.view registerClass:[ImageCollectionViewCell class] forCellWithReuseIdentifier:kReuseIdentifier];
|
||||
}
|
||||
|
||||
- (void)reloadTapped
|
||||
@@ -85,6 +87,11 @@ static NSUInteger kNumberOfImages = 14;
|
||||
|
||||
- (ASCellNodeBlock)collectionNode:(ASCollectionNode *)collectionNode nodeBlockForItemAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
if (kShowUICollectionViewCells && indexPath.item % 3 == 1) {
|
||||
// When enabled, return nil for every third cell and then cellForItemAtIndexPath: will be called.
|
||||
return nil;
|
||||
}
|
||||
|
||||
UIImage *image = _sections[indexPath.section][indexPath.item];
|
||||
return ^{
|
||||
return [[ImageCellNode alloc] initWithImage:image];
|
||||
@@ -92,6 +99,31 @@ static NSUInteger kNumberOfImages = 14;
|
||||
}
|
||||
|
||||
|
||||
- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
MosaicCollectionViewLayout *layout = (MosaicCollectionViewLayout *)[collectionView collectionViewLayout];
|
||||
return ASSizeRangeMake(CGSizeZero, [layout itemSizeAtIndexPath:indexPath]);
|
||||
}
|
||||
|
||||
- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
MosaicCollectionViewLayout *layout = (MosaicCollectionViewLayout *)[collectionView collectionViewLayout];
|
||||
return ASSizeRangeMake(CGSizeZero, [layout headerSizeForSection:indexPath.section]);
|
||||
}
|
||||
|
||||
- (ASScrollDirection)scrollableDirections
|
||||
{
|
||||
return ASScrollDirectionVerticalDirections;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asks the inspector for the number of supplementary views for the given kind in the specified section.
|
||||
*/
|
||||
- (NSUInteger)collectionView:(ASCollectionView *)collectionView supplementaryNodesOfKind:(NSString *)kind inSection:(NSUInteger)section
|
||||
{
|
||||
return [kind isEqualToString:UICollectionElementKindSectionHeader] ? 1 : 0;
|
||||
}
|
||||
|
||||
- (ASCellNode *)collectionNode:(ASCollectionNode *)collectionNode nodeForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
NSDictionary *textAttributes = @{
|
||||
@@ -116,7 +148,22 @@ static NSUInteger kNumberOfImages = 14;
|
||||
|
||||
- (CGSize)collectionView:(ASCollectionNode *)collectionNode layout:(UICollectionViewLayout *)collectionViewLayout originalItemSizeAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
return [(UIImage *)_sections[indexPath.section][indexPath.item] size];
|
||||
ASCellNode *cellNode = [collectionNode nodeForItemAtIndexPath:indexPath];
|
||||
if ([cellNode isKindOfClass:[ImageCellNode class]]) {
|
||||
return [[(ImageCellNode *)cellNode image] size];
|
||||
} else {
|
||||
return CGSizeMake(100, 100); // In kShowUICollectionViewCells = YES mode, make those cells 100x100.
|
||||
}
|
||||
}
|
||||
|
||||
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
return [_collectionNode.view dequeueReusableCellWithReuseIdentifier:kReuseIdentifier forIndexPath:indexPath];
|
||||
}
|
||||
|
||||
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
return nil;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user