feat:add CollectionView
This commit is contained in:
10
iOS/Pod/Classes/Shader/DoricCollectionItemNode.h
Normal file
10
iOS/Pod/Classes/Shader/DoricCollectionItemNode.h
Normal file
@@ -0,0 +1,10 @@
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/11/28.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "DoricStackNode.h"
|
||||
|
||||
|
||||
@interface DoricCollectionItemNode : DoricStackNode
|
||||
@end
|
33
iOS/Pod/Classes/Shader/DoricCollectionItemNode.m
Normal file
33
iOS/Pod/Classes/Shader/DoricCollectionItemNode.m
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/11/28.
|
||||
//
|
||||
|
||||
#import "DoricCollectionItemNode.h"
|
||||
|
||||
@interface DoricCollectionItemView : DoricStackView
|
||||
@end
|
||||
|
||||
@implementation DoricCollectionItemView
|
||||
@end
|
||||
|
||||
@interface DoricCollectionItemNode ()
|
||||
@end
|
||||
|
||||
|
||||
@implementation DoricCollectionItemNode
|
||||
- (instancetype)initWithContext:(DoricContext *)doricContext {
|
||||
if (self = [super initWithContext:doricContext]) {
|
||||
self.reusable = YES;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)initWithSuperNode:(DoricSuperNode *)superNode {
|
||||
[super initWithSuperNode:superNode];
|
||||
self.reusable = YES;
|
||||
}
|
||||
|
||||
- (DoricStackView *)build {
|
||||
return [DoricCollectionItemView new];
|
||||
}
|
||||
@end
|
9
iOS/Pod/Classes/Shader/DoricCollectionNode.h
Normal file
9
iOS/Pod/Classes/Shader/DoricCollectionNode.h
Normal file
@@ -0,0 +1,9 @@
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/11/28.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "DoricSuperNode.h"
|
||||
|
||||
@interface DoricCollectionNode : DoricSuperNode<UICollectionView *>
|
||||
@end
|
71
iOS/Pod/Classes/Shader/DoricCollectionNode.m
Normal file
71
iOS/Pod/Classes/Shader/DoricCollectionNode.m
Normal file
@@ -0,0 +1,71 @@
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/11/28.
|
||||
//
|
||||
|
||||
#import "DoricCollectionNode.h"
|
||||
#import "DoricCollectionItemNode.h"
|
||||
#import "DoricExtensions.h"
|
||||
|
||||
@interface DoricCollectionViewCell : UICollectionViewCell
|
||||
@property(nonatomic, strong) DoricCollectionItemNode *viewNode;
|
||||
@end
|
||||
|
||||
@implementation DoricCollectionViewCell
|
||||
@end
|
||||
|
||||
@interface DoricCollectionView : UICollectionView
|
||||
@end
|
||||
|
||||
@implementation DoricCollectionView
|
||||
- (CGSize)sizeThatFits:(CGSize)size {
|
||||
if (self.subviews.count > 0) {
|
||||
CGFloat width = size.width;
|
||||
CGFloat height = size.height;
|
||||
for (UIView *child in self.subviews) {
|
||||
CGSize childSize = [child measureSize:size];
|
||||
width = MAX(childSize.width, width);
|
||||
height = MAX(childSize.height, height);
|
||||
}
|
||||
return CGSizeMake(width, height);
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
- (void)layoutSelf:(CGSize)targetSize {
|
||||
[super layoutSelf:targetSize];
|
||||
[self reloadData];
|
||||
}
|
||||
@end
|
||||
|
||||
@interface DoricCollectionNode () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
|
||||
@property(nonatomic, strong) NSMutableDictionary <NSNumber *, NSString *> *itemViewIds;
|
||||
@property(nonatomic, assign) NSUInteger itemCount;
|
||||
@property(nonatomic, assign) NSUInteger batchCount;
|
||||
@end
|
||||
|
||||
@implementation DoricCollectionNode
|
||||
- (instancetype)initWithContext:(DoricContext *)doricContext {
|
||||
if (self = [super initWithContext:doricContext]) {
|
||||
_itemViewIds = [NSMutableDictionary new];
|
||||
_batchCount = 15;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (UICollectionView *)build {
|
||||
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
|
||||
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
|
||||
|
||||
return [[[DoricCollectionView alloc] initWithFrame:CGRectZero
|
||||
collectionViewLayout:flowLayout]
|
||||
also:^(UICollectionView *it) {
|
||||
it.backgroundColor = [UIColor whiteColor];
|
||||
it.pagingEnabled = YES;
|
||||
it.delegate = self;
|
||||
it.dataSource = self;
|
||||
[it registerClass:[DoricCollectionViewCell class] forCellWithReuseIdentifier:@"doricCell"];
|
||||
}];
|
||||
}
|
||||
|
||||
|
||||
@end
|
@@ -24,11 +24,11 @@
|
||||
#import "Doric.h"
|
||||
#import "DoricSlideItemNode.h"
|
||||
|
||||
@interface DoricCollectionViewCell : UICollectionViewCell
|
||||
@interface DoricSliderViewCell : UICollectionViewCell
|
||||
@property(nonatomic, strong) DoricSlideItemNode *doricSlideItemNode;
|
||||
@end
|
||||
|
||||
@implementation DoricCollectionViewCell
|
||||
@implementation DoricSliderViewCell
|
||||
@end
|
||||
|
||||
@interface DoricSliderNode () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
|
||||
@@ -37,18 +37,20 @@ @interface DoricSliderNode () <UICollectionViewDataSource, UICollectionViewDeleg
|
||||
@property(nonatomic, assign) NSUInteger batchCount;
|
||||
@end
|
||||
|
||||
@interface DoricCollectionView : UICollectionView
|
||||
@interface DoricSliderView : UICollectionView
|
||||
@end
|
||||
|
||||
@implementation DoricCollectionView
|
||||
@implementation DoricSliderView
|
||||
- (CGSize)sizeThatFits:(CGSize)size {
|
||||
if (self.subviews.count > 0) {
|
||||
CGFloat width = size.width;
|
||||
CGFloat height = size.height;
|
||||
for (UIView *child in self.subviews) {
|
||||
CGSize childSize = [child measureSize:size];
|
||||
width = MAX(childSize.width, width);
|
||||
height = MAX(childSize.height, height);
|
||||
}
|
||||
return CGSizeMake(size.width, size.height);
|
||||
return CGSizeMake(width, height);
|
||||
}
|
||||
return size;
|
||||
}
|
||||
@@ -72,14 +74,14 @@ - (UICollectionView *)build {
|
||||
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
|
||||
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
|
||||
|
||||
return [[[DoricCollectionView alloc] initWithFrame:CGRectZero
|
||||
collectionViewLayout:flowLayout]
|
||||
return [[[DoricSliderView alloc] initWithFrame:CGRectZero
|
||||
collectionViewLayout:flowLayout]
|
||||
also:^(UICollectionView *it) {
|
||||
it.backgroundColor = [UIColor whiteColor];
|
||||
it.pagingEnabled = YES;
|
||||
it.delegate = self;
|
||||
it.dataSource = self;
|
||||
[it registerClass:[DoricCollectionViewCell class] forCellWithReuseIdentifier:@"doricCell"];
|
||||
[it registerClass:[DoricSliderViewCell class] forCellWithReuseIdentifier:@"doricCell"];
|
||||
}];
|
||||
}
|
||||
|
||||
@@ -118,7 +120,7 @@ - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collection
|
||||
NSUInteger position = (NSUInteger) indexPath.row;
|
||||
NSDictionary *model = [self itemModelAt:position];
|
||||
NSDictionary *props = model[@"props"];
|
||||
DoricCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"doricCell" forIndexPath:indexPath];
|
||||
DoricSliderViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"doricCell" forIndexPath:indexPath];
|
||||
if (!cell.doricSlideItemNode) {
|
||||
DoricSlideItemNode *slideItemNode = [[DoricSlideItemNode alloc] initWithContext:self.doricContext];
|
||||
[slideItemNode initWithSuperNode:self];
|
||||
@@ -159,8 +161,8 @@ - (DoricViewNode *)subNodeWithViewId:(NSString *)viewId {
|
||||
__block DoricViewNode *ret = nil;
|
||||
[self.doricContext.driver ensureSyncInMainQueue:^{
|
||||
for (UICollectionViewCell *collectionViewCell in self.view.visibleCells) {
|
||||
if ([collectionViewCell isKindOfClass:[DoricCollectionViewCell class]]) {
|
||||
DoricSlideItemNode *node = ((DoricCollectionViewCell *) collectionViewCell).doricSlideItemNode;
|
||||
if ([collectionViewCell isKindOfClass:[DoricSliderViewCell class]]) {
|
||||
DoricSlideItemNode *node = ((DoricSliderViewCell *) collectionViewCell).doricSlideItemNode;
|
||||
if ([viewId isEqualToString:node.viewId]) {
|
||||
ret = node;
|
||||
break;
|
||||
|
Reference in New Issue
Block a user