feat:add scrollview on iOS
This commit is contained in:
@@ -30,6 +30,7 @@
|
||||
#import "DoricImageNode.h"
|
||||
#import "DoricListNode.h"
|
||||
#import "DoricListItemNode.h"
|
||||
#import "DoricScrollerNode.h"
|
||||
|
||||
@interface DoricRegistry ()
|
||||
|
||||
@@ -62,6 +63,7 @@ - (void)innerRegister {
|
||||
[self registerViewNode:DoricImageNode.class withName:@"Image"];
|
||||
[self registerViewNode:DoricListNode.class withName:@"List"];
|
||||
[self registerViewNode:DoricListItemNode.class withName:@"ListItem"];
|
||||
[self registerViewNode:DoricScrollerNode.class withName:@"Scroller"];
|
||||
}
|
||||
|
||||
- (void)registerJSBundle:(NSString *)bundle withName:(NSString *)name {
|
||||
|
@@ -25,7 +25,6 @@
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface DoricGroupNode <V:UIView *> : DoricSuperNode<V>
|
||||
@property(nonatomic, assign) BOOL reusable;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
@@ -34,7 +34,6 @@ - (instancetype)initWithContext:(DoricContext *)doricContext {
|
||||
if (self = [super initWithContext:doricContext]) {
|
||||
_childNodes = @[];
|
||||
_childViewIds = @[];
|
||||
_reusable = NO;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
@@ -38,6 +38,11 @@ - (instancetype)initWithContext:(DoricContext *)doricContext {
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)initWithSuperNode:(DoricSuperNode *)superNode {
|
||||
[super initWithSuperNode:superNode];
|
||||
self.reusable = YES;
|
||||
}
|
||||
|
||||
- (DoricStackView *)build {
|
||||
return [DoricListItemView new];
|
||||
}
|
||||
|
9
iOS/Pod/Classes/Shader/DoricScrollerNode.h
Normal file
9
iOS/Pod/Classes/Shader/DoricScrollerNode.h
Normal file
@@ -0,0 +1,9 @@
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/11/19.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "DoricSuperNode.h"
|
||||
|
||||
@interface DoricScrollerNode : DoricSuperNode<UIScrollView *>
|
||||
@end
|
90
iOS/Pod/Classes/Shader/DoricScrollerNode.m
Normal file
90
iOS/Pod/Classes/Shader/DoricScrollerNode.m
Normal file
@@ -0,0 +1,90 @@
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/11/19.
|
||||
//
|
||||
|
||||
#import "DoricScrollerNode.h"
|
||||
#import "DoricExtensions.h"
|
||||
|
||||
@interface DoricScrollView : UIScrollView
|
||||
@end
|
||||
|
||||
@implementation DoricScrollView
|
||||
|
||||
- (void)layoutSubviews {
|
||||
[super layoutSubviews];
|
||||
if (self.subviews.count > 0) {
|
||||
UIView *child = self.subviews[0];
|
||||
[self setContentSize:child.frame.size];
|
||||
}
|
||||
}
|
||||
|
||||
- (CGSize)sizeThatFits:(CGSize)size {
|
||||
if (self.subviews.count > 0) {
|
||||
UIView *child = self.subviews[0];
|
||||
CGSize childSize = [child sizeThatFits:size];
|
||||
return CGSizeMake(MIN(size.width, childSize.width), MIN(size.height, childSize.height));
|
||||
}
|
||||
return CGSizeZero;
|
||||
}
|
||||
@end
|
||||
|
||||
@interface DoricScrollerNode ()
|
||||
@property(nonatomic, strong) DoricViewNode *childNode;
|
||||
@property(nonatomic, copy) NSString *childViewId;
|
||||
@end
|
||||
|
||||
@implementation DoricScrollerNode
|
||||
- (UIScrollView *)build {
|
||||
return [DoricScrollView new];
|
||||
}
|
||||
|
||||
- (void)blend:(NSDictionary *)props {
|
||||
[super blend:props];
|
||||
NSDictionary *childModel = [self subModelOf:self.childViewId];
|
||||
if (!childModel) {
|
||||
return;
|
||||
}
|
||||
NSString *viewId = childModel[@"id"];
|
||||
NSString *type = childModel[@"type"];
|
||||
NSDictionary *childProps = childModel[@"props"];
|
||||
if (self.childNode) {
|
||||
if ([self.childNode.viewId isEqualToString:viewId]) {
|
||||
//skip
|
||||
} else {
|
||||
if (self.reusable && [type isEqualToString:self.childNode.type]) {
|
||||
[self.childNode also:^(DoricViewNode *it) {
|
||||
it.viewId = viewId;
|
||||
[it blend:childProps];
|
||||
}];
|
||||
} else {
|
||||
[self.childNode.view removeFromSuperview];
|
||||
self.childNode = [[DoricViewNode create:self.doricContext withType:type] also:^(DoricViewNode *it) {
|
||||
it.viewId = viewId;
|
||||
[it initWithSuperNode:self];
|
||||
[it blend:childProps];
|
||||
[self.view addSubview:it.view];
|
||||
}];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.childNode = [[DoricViewNode create:self.doricContext withType:type] also:^(DoricViewNode *it) {
|
||||
it.viewId = viewId;
|
||||
[it initWithSuperNode:self];
|
||||
[it blend:childProps];
|
||||
[self.view addSubview:it.view];
|
||||
}];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)blendView:(UIScrollView *)view forPropName:(NSString *)name propValue:(id)prop {
|
||||
if ([@"content" isEqualToString:name]) {
|
||||
self.childViewId = prop;
|
||||
} else {
|
||||
[super blendView:view forPropName:name propValue:prop];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)blendSubNode:(NSDictionary *)subModel {
|
||||
[self.childNode blend:subModel];
|
||||
}
|
||||
@end
|
@@ -21,6 +21,8 @@
|
||||
#import "DoricViewNode.h"
|
||||
|
||||
@interface DoricSuperNode<V:UIView *> : DoricViewNode<V>
|
||||
@property(nonatomic, assign) BOOL reusable;
|
||||
|
||||
- (DoricLayoutConfig *)generateDefaultLayoutParams;
|
||||
|
||||
- (void)blendSubNode:(DoricViewNode *)subNode layoutConfig:(NSDictionary *)layoutConfig;
|
||||
|
@@ -81,6 +81,9 @@ - (instancetype)initWithContext:(DoricContext *)doricContext {
|
||||
|
||||
|
||||
- (void)initWithSuperNode:(DoricSuperNode *)superNode {
|
||||
if ([self isKindOfClass:[DoricSuperNode class]]) {
|
||||
((DoricSuperNode *) self).reusable = superNode.reusable;
|
||||
}
|
||||
self.superNode = superNode;
|
||||
self.view = [[self build] also:^(UIView *it) {
|
||||
it.layoutConfig = [superNode generateDefaultLayoutParams];
|
||||
|
Reference in New Issue
Block a user