feat:add scrollview on iOS
This commit is contained in:
parent
9a25e14dc9
commit
f0430a32a7
@ -78,9 +78,6 @@ public abstract class GroupNode<F extends ViewGroup> extends SuperNode<F> {
|
|||||||
mView.removeView(oldNode.getDoricLayer());
|
mView.removeView(oldNode.getDoricLayer());
|
||||||
ViewNode newNode = ViewNode.create(getDoricContext(), type);
|
ViewNode newNode = ViewNode.create(getDoricContext(), type);
|
||||||
newNode.setId(id);
|
newNode.setId(id);
|
||||||
if (newNode instanceof SuperNode) {
|
|
||||||
((SuperNode) newNode).mReusable = this.mReusable;
|
|
||||||
}
|
|
||||||
newNode.init(this);
|
newNode.init(this);
|
||||||
newNode.blend(model.getProperty("props").asObject());
|
newNode.blend(model.getProperty("props").asObject());
|
||||||
mChildNodes.add(idx, newNode);
|
mChildNodes.add(idx, newNode);
|
||||||
@ -124,9 +121,6 @@ public abstract class GroupNode<F extends ViewGroup> extends SuperNode<F> {
|
|||||||
//Insert
|
//Insert
|
||||||
ViewNode newNode = ViewNode.create(getDoricContext(), type);
|
ViewNode newNode = ViewNode.create(getDoricContext(), type);
|
||||||
newNode.setId(id);
|
newNode.setId(id);
|
||||||
if (newNode instanceof SuperNode) {
|
|
||||||
((SuperNode) newNode).mReusable = this.mReusable;
|
|
||||||
}
|
|
||||||
newNode.init(this);
|
newNode.init(this);
|
||||||
newNode.blend(model.getProperty("props").asObject());
|
newNode.blend(model.getProperty("props").asObject());
|
||||||
mChildNodes.add(newNode);
|
mChildNodes.add(newNode);
|
||||||
|
@ -55,6 +55,9 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
|
|||||||
private DoricLayer doricLayer;
|
private DoricLayer doricLayer;
|
||||||
|
|
||||||
public void init(SuperNode superNode) {
|
public void init(SuperNode superNode) {
|
||||||
|
if (this instanceof SuperNode) {
|
||||||
|
((SuperNode<T>) this).mReusable = superNode.mReusable;
|
||||||
|
}
|
||||||
this.mSuperNode = superNode;
|
this.mSuperNode = superNode;
|
||||||
this.mLayoutParams = superNode.generateDefaultLayoutParams();
|
this.mLayoutParams = superNode.generateDefaultLayoutParams();
|
||||||
this.doricLayer = new DoricLayer(getContext());
|
this.doricLayer = new DoricLayer(getContext());
|
||||||
|
@ -37,8 +37,7 @@ class ScrollerPanel extends Panel {
|
|||||||
width: 80,
|
width: 80,
|
||||||
height: 50,
|
height: 50,
|
||||||
})),
|
})),
|
||||||
]
|
]).also(it => it.space = 20)),
|
||||||
)),
|
|
||||||
hlayout([
|
hlayout([
|
||||||
...[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5].map(e => text({
|
...[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5].map(e => text({
|
||||||
text: colors[e % colors.length],
|
text: colors[e % colors.length],
|
||||||
@ -55,6 +54,6 @@ class ScrollerPanel extends Panel {
|
|||||||
]
|
]
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
)))
|
).also(it => it.space = 20)))
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -21,6 +21,7 @@
|
|||||||
E2334B0822E9D2070098A085 /* ExampleTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E2334B0722E9D2070098A085 /* ExampleTests.m */; };
|
E2334B0822E9D2070098A085 /* ExampleTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E2334B0722E9D2070098A085 /* ExampleTests.m */; };
|
||||||
E2334B1322E9D2070098A085 /* ExampleUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = E2334B1222E9D2070098A085 /* ExampleUITests.m */; };
|
E2334B1322E9D2070098A085 /* ExampleUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = E2334B1222E9D2070098A085 /* ExampleUITests.m */; };
|
||||||
E2BF7BF7237E8E9F001B0EDC /* ListDemo.js in Resources */ = {isa = PBXBuildFile; fileRef = E2BF7BF6237E8E9F001B0EDC /* ListDemo.js */; };
|
E2BF7BF7237E8E9F001B0EDC /* ListDemo.js in Resources */ = {isa = PBXBuildFile; fileRef = E2BF7BF6237E8E9F001B0EDC /* ListDemo.js */; };
|
||||||
|
E2F447F42383924B00073C7F /* ScrollerDemo.js in Resources */ = {isa = PBXBuildFile; fileRef = E2F447F32383924B00073C7F /* ScrollerDemo.js */; };
|
||||||
/* End PBXBuildFile section */
|
/* End PBXBuildFile section */
|
||||||
|
|
||||||
/* Begin PBXContainerItemProxy section */
|
/* Begin PBXContainerItemProxy section */
|
||||||
@ -69,6 +70,7 @@
|
|||||||
E2334B1222E9D2070098A085 /* ExampleUITests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ExampleUITests.m; sourceTree = "<group>"; };
|
E2334B1222E9D2070098A085 /* ExampleUITests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ExampleUITests.m; sourceTree = "<group>"; };
|
||||||
E2334B1422E9D2070098A085 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
E2334B1422E9D2070098A085 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
||||||
E2BF7BF6237E8E9F001B0EDC /* ListDemo.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = ListDemo.js; sourceTree = "<group>"; };
|
E2BF7BF6237E8E9F001B0EDC /* ListDemo.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = ListDemo.js; sourceTree = "<group>"; };
|
||||||
|
E2F447F32383924B00073C7F /* ScrollerDemo.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = ScrollerDemo.js; sourceTree = "<group>"; };
|
||||||
/* End PBXFileReference section */
|
/* End PBXFileReference section */
|
||||||
|
|
||||||
/* Begin PBXFrameworksBuildPhase section */
|
/* Begin PBXFrameworksBuildPhase section */
|
||||||
@ -125,6 +127,7 @@
|
|||||||
E21DC9D12302865E00660C5C /* src */ = {
|
E21DC9D12302865E00660C5C /* src */ = {
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
|
E2F447F32383924B00073C7F /* ScrollerDemo.js */,
|
||||||
E2BF7BF6237E8E9F001B0EDC /* ListDemo.js */,
|
E2BF7BF6237E8E9F001B0EDC /* ListDemo.js */,
|
||||||
E21DC9D22302865E00660C5C /* Snake.js */,
|
E21DC9D22302865E00660C5C /* Snake.js */,
|
||||||
E21DC9D32302865E00660C5C /* Counter.js */,
|
E21DC9D32302865E00660C5C /* Counter.js */,
|
||||||
@ -298,6 +301,7 @@
|
|||||||
isa = PBXResourcesBuildPhase;
|
isa = PBXResourcesBuildPhase;
|
||||||
buildActionMask = 2147483647;
|
buildActionMask = 2147483647;
|
||||||
files = (
|
files = (
|
||||||
|
E2F447F42383924B00073C7F /* ScrollerDemo.js in Resources */,
|
||||||
E2BF7BF7237E8E9F001B0EDC /* ListDemo.js in Resources */,
|
E2BF7BF7237E8E9F001B0EDC /* ListDemo.js in Resources */,
|
||||||
E21DC9D42302870000660C5C /* Snake.js in Resources */,
|
E21DC9D42302870000660C5C /* Snake.js in Resources */,
|
||||||
E21DC9D52302870000660C5C /* Counter.js in Resources */,
|
E21DC9D52302870000660C5C /* Counter.js in Resources */,
|
||||||
|
@ -26,7 +26,7 @@ @implementation ViewController
|
|||||||
- (void)viewDidLoad {
|
- (void)viewDidLoad {
|
||||||
[super viewDidLoad];
|
[super viewDidLoad];
|
||||||
|
|
||||||
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"ListDemo" ofType:@"js"];
|
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"ScrollerDemo" ofType:@"js"];
|
||||||
NSString *jsContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
|
NSString *jsContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
|
||||||
self.doricContext = [[DoricContext alloc] initWithScript:jsContent source:@"test.js"];
|
self.doricContext = [[DoricContext alloc] initWithScript:jsContent source:@"test.js"];
|
||||||
[self.doricContext.rootNode setupRootView:[[DoricStackView new] also:^(DoricStackView *it) {
|
[self.doricContext.rootNode setupRootView:[[DoricStackView new] also:^(DoricStackView *it) {
|
||||||
|
@ -30,6 +30,7 @@
|
|||||||
#import "DoricImageNode.h"
|
#import "DoricImageNode.h"
|
||||||
#import "DoricListNode.h"
|
#import "DoricListNode.h"
|
||||||
#import "DoricListItemNode.h"
|
#import "DoricListItemNode.h"
|
||||||
|
#import "DoricScrollerNode.h"
|
||||||
|
|
||||||
@interface DoricRegistry ()
|
@interface DoricRegistry ()
|
||||||
|
|
||||||
@ -62,6 +63,7 @@ - (void)innerRegister {
|
|||||||
[self registerViewNode:DoricImageNode.class withName:@"Image"];
|
[self registerViewNode:DoricImageNode.class withName:@"Image"];
|
||||||
[self registerViewNode:DoricListNode.class withName:@"List"];
|
[self registerViewNode:DoricListNode.class withName:@"List"];
|
||||||
[self registerViewNode:DoricListItemNode.class withName:@"ListItem"];
|
[self registerViewNode:DoricListItemNode.class withName:@"ListItem"];
|
||||||
|
[self registerViewNode:DoricScrollerNode.class withName:@"Scroller"];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)registerJSBundle:(NSString *)bundle withName:(NSString *)name {
|
- (void)registerJSBundle:(NSString *)bundle withName:(NSString *)name {
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
NS_ASSUME_NONNULL_BEGIN
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
@interface DoricGroupNode <V:UIView *> : DoricSuperNode<V>
|
@interface DoricGroupNode <V:UIView *> : DoricSuperNode<V>
|
||||||
@property(nonatomic, assign) BOOL reusable;
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_END
|
NS_ASSUME_NONNULL_END
|
||||||
|
@ -34,7 +34,6 @@ - (instancetype)initWithContext:(DoricContext *)doricContext {
|
|||||||
if (self = [super initWithContext:doricContext]) {
|
if (self = [super initWithContext:doricContext]) {
|
||||||
_childNodes = @[];
|
_childNodes = @[];
|
||||||
_childViewIds = @[];
|
_childViewIds = @[];
|
||||||
_reusable = NO;
|
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,11 @@ - (instancetype)initWithContext:(DoricContext *)doricContext {
|
|||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)initWithSuperNode:(DoricSuperNode *)superNode {
|
||||||
|
[super initWithSuperNode:superNode];
|
||||||
|
self.reusable = YES;
|
||||||
|
}
|
||||||
|
|
||||||
- (DoricStackView *)build {
|
- (DoricStackView *)build {
|
||||||
return [DoricListItemView new];
|
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"
|
#import "DoricViewNode.h"
|
||||||
|
|
||||||
@interface DoricSuperNode<V:UIView *> : DoricViewNode<V>
|
@interface DoricSuperNode<V:UIView *> : DoricViewNode<V>
|
||||||
|
@property(nonatomic, assign) BOOL reusable;
|
||||||
|
|
||||||
- (DoricLayoutConfig *)generateDefaultLayoutParams;
|
- (DoricLayoutConfig *)generateDefaultLayoutParams;
|
||||||
|
|
||||||
- (void)blendSubNode:(DoricViewNode *)subNode layoutConfig:(NSDictionary *)layoutConfig;
|
- (void)blendSubNode:(DoricViewNode *)subNode layoutConfig:(NSDictionary *)layoutConfig;
|
||||||
|
@ -81,6 +81,9 @@ - (instancetype)initWithContext:(DoricContext *)doricContext {
|
|||||||
|
|
||||||
|
|
||||||
- (void)initWithSuperNode:(DoricSuperNode *)superNode {
|
- (void)initWithSuperNode:(DoricSuperNode *)superNode {
|
||||||
|
if ([self isKindOfClass:[DoricSuperNode class]]) {
|
||||||
|
((DoricSuperNode *) self).reusable = superNode.reusable;
|
||||||
|
}
|
||||||
self.superNode = superNode;
|
self.superNode = superNode;
|
||||||
self.view = [[self build] also:^(UIView *it) {
|
self.view = [[self build] also:^(UIView *it) {
|
||||||
it.layoutConfig = [superNode generateDefaultLayoutParams];
|
it.layoutConfig = [superNode generateDefaultLayoutParams];
|
||||||
|
Reference in New Issue
Block a user