iOS render stack and soon
This commit is contained in:
parent
674335324b
commit
b0e34a316b
@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
NS_ASSUME_NONNULL_BEGIN
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
@interface DoricGroupNode : DoricViewNode<UIView *>
|
@interface DoricGroupNode <V:UIView *,P:LayoutParams *>: DoricViewNode<V>
|
||||||
|
|
||||||
@property (nonatomic,strong) NSMutableDictionary *children;
|
@property (nonatomic,strong) NSMutableDictionary *children;
|
||||||
@property (nonatomic,strong) NSMutableArray *indexedChildren;
|
@property (nonatomic,strong) NSMutableArray *indexedChildren;
|
||||||
@ -19,7 +19,7 @@ NS_ASSUME_NONNULL_BEGIN
|
|||||||
|
|
||||||
- (void)blendChild:(DoricViewNode *)child layoutConfig:(NSDictionary *)layoutconfig;
|
- (void)blendChild:(DoricViewNode *)child layoutConfig:(NSDictionary *)layoutconfig;
|
||||||
|
|
||||||
- (LayoutParams *)generateDefaultLayoutParams;
|
- (P)generateDefaultLayoutParams;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_END
|
NS_ASSUME_NONNULL_END
|
||||||
|
@ -9,14 +9,13 @@
|
|||||||
|
|
||||||
@implementation DoricGroupNode
|
@implementation DoricGroupNode
|
||||||
|
|
||||||
- (instancetype)init {
|
- (instancetype)initWithContext:(DoricContext *)doricContext {
|
||||||
if(self = [super init]) {
|
if(self = [super initWithContext:doricContext]) {
|
||||||
_children = [[NSMutableDictionary alloc] init];
|
_children = [[NSMutableDictionary alloc] init];
|
||||||
_indexedChildren = [[NSMutableArray alloc] init];
|
_indexedChildren = [[NSMutableArray alloc] init];
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (UIView *)build:(NSDictionary *)props {
|
- (UIView *)build:(NSDictionary *)props {
|
||||||
return [[UIView alloc] init];
|
return [[UIView alloc] init];
|
||||||
}
|
}
|
||||||
@ -40,26 +39,34 @@ - (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop
|
|||||||
node.viewId = viewId;
|
node.viewId = viewId;
|
||||||
[self.children setObject:node forKey:viewId];
|
[self.children setObject:node forKey:viewId];
|
||||||
} else if (i != node.index){
|
} else if (i != node.index){
|
||||||
|
[self.indexedChildren removeObjectAtIndex:i];
|
||||||
node.index = i;
|
node.index = i;
|
||||||
[node.view removeFromSuperview];
|
[node.view removeFromSuperview];
|
||||||
}
|
}
|
||||||
LayoutParams *params = node.layoutParams;
|
LayoutParams *params = node.layoutParams;
|
||||||
if (params == nil) {
|
if (params == nil) {
|
||||||
params = [self generateDefaultLayoutParams];
|
params = [self generateDefaultLayoutParams];
|
||||||
|
node.layoutParams = params;
|
||||||
}
|
}
|
||||||
[node blend:[val objectForKey:@"props"]];
|
[node blend:[val objectForKey:@"props"]];
|
||||||
if ([self.indexedChildren objectAtIndex:i] == nil) {
|
if (self.indexedChildren.count <= i) {
|
||||||
|
[self.view addSubview:node.view];
|
||||||
|
[self.indexedChildren addObject:node];
|
||||||
|
}else if ([self.indexedChildren objectAtIndex:i] == nil) {
|
||||||
|
self.indexedChildren[i] = node;
|
||||||
[self.view insertSubview:node.view atIndex:i];
|
[self.view insertSubview:node.view atIndex:i];
|
||||||
[self.indexedChildren setObject:node atIndexedSubscript:i];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while (i < self.view.subviews.count) {
|
while (i < self.view.subviews.count) {
|
||||||
[self.view.subviews[i] removeFromSuperview];
|
UIView *view = self.view.subviews[i];
|
||||||
|
if (i < self.indexedChildren.count) {
|
||||||
DoricViewNode *node = [self.indexedChildren objectAtIndex:i];
|
DoricViewNode *node = [self.indexedChildren objectAtIndex:i];
|
||||||
if (node != nil) {
|
if (node && node.view == view) {
|
||||||
[self.children removeObjectForKey: node.viewId];
|
[self.children removeObjectForKey: node.viewId];
|
||||||
[self.indexedChildren removeObjectAtIndex:i];
|
[self.indexedChildren removeObjectAtIndex:i];
|
||||||
|
[view removeFromSuperview];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
@ -75,13 +82,17 @@ - (LayoutParams *)generateDefaultLayoutParams {
|
|||||||
|
|
||||||
- (void)blendChild:(DoricViewNode *)child layoutConfig:(NSDictionary *)layoutconfig {
|
- (void)blendChild:(DoricViewNode *)child layoutConfig:(NSDictionary *)layoutconfig {
|
||||||
LayoutParams *params = child.layoutParams;
|
LayoutParams *params = child.layoutParams;
|
||||||
|
if ([params isKindOfClass:MarginLayoutParams.class]) {
|
||||||
|
MarginLayoutParams *marginParams = (MarginLayoutParams *)params;
|
||||||
NSDictionary *margin = [layoutconfig objectForKey:@"margin"];
|
NSDictionary *margin = [layoutconfig objectForKey:@"margin"];
|
||||||
if (margin) {
|
if (margin) {
|
||||||
params.margin.top = [(NSNumber *)[margin objectForKey:@"top"] floatValue];
|
marginParams.margin.top = [(NSNumber *)[margin objectForKey:@"top"] floatValue];
|
||||||
params.margin.left = [(NSNumber *)[margin objectForKey:@"left"] floatValue];
|
marginParams.margin.left = [(NSNumber *)[margin objectForKey:@"left"] floatValue];
|
||||||
params.margin.right = [(NSNumber *)[margin objectForKey:@"right"] floatValue];
|
marginParams.margin.right = [(NSNumber *)[margin objectForKey:@"right"] floatValue];
|
||||||
params.margin.bottom = [(NSNumber *)[margin objectForKey:@"bottom"] floatValue];
|
marginParams.margin.bottom = [(NSNumber *)[margin objectForKey:@"bottom"] floatValue];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
NS_ASSUME_NONNULL_BEGIN
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
@interface DoricHLayoutNode : DoricGroupNode
|
@interface DoricHLayoutNode : DoricGroupNode<UIView *,VHLayoutParams *>
|
||||||
@property (nonatomic) CGFloat space;
|
@property (nonatomic) CGFloat space;
|
||||||
@property (nonatomic) DoricGravity gravity;
|
@property (nonatomic) DoricGravity gravity;
|
||||||
@end
|
@end
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#import "DoricHLayoutNode.h"
|
#import "DoricHLayoutNode.h"
|
||||||
|
#import "DoricUtil.h"
|
||||||
|
|
||||||
@implementation DoricHLayoutNode
|
@implementation DoricHLayoutNode
|
||||||
- (instancetype)init {
|
- (instancetype)init {
|
||||||
@ -16,15 +17,48 @@ - (instancetype)init {
|
|||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)blendView:(id)view forPropName:(NSString *)name propValue:(id)prop {
|
||||||
|
if ([name isEqualToString:@"gravity"]) {
|
||||||
|
self.gravity = [(NSNumber *)prop integerValue];
|
||||||
|
} else if ([name isEqualToString:@"space"]) {
|
||||||
|
self.space = [(NSNumber *)prop floatValue];
|
||||||
|
} else {
|
||||||
|
[super blendView:view forPropName:name propValue:prop];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)blendChild:(DoricViewNode *)child layoutConfig:(NSDictionary *)layoutconfig {
|
||||||
|
[super blendChild:child layoutConfig:layoutconfig];
|
||||||
|
if (![child.layoutParams isKindOfClass:VHLayoutParams.class]) {
|
||||||
|
DoricLog(@"blend VLayout child error,layout params not match");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
VHLayoutParams *params = (VHLayoutParams *)child.layoutParams;
|
||||||
|
NSDictionary *margin = [layoutconfig objectForKey:@"margin"];
|
||||||
|
if (margin) {
|
||||||
|
params.margin.top = [(NSNumber *)[margin objectForKey:@"top"] floatValue];
|
||||||
|
params.margin.left = [(NSNumber *)[margin objectForKey:@"left"] floatValue];
|
||||||
|
params.margin.right = [(NSNumber *)[margin objectForKey:@"right"] floatValue];
|
||||||
|
params.margin.bottom = [(NSNumber *)[margin objectForKey:@"bottom"] floatValue];
|
||||||
|
}
|
||||||
|
NSNumber *alignment = [layoutconfig objectForKey:@"alignment"];
|
||||||
|
if (alignment) {
|
||||||
|
params.alignment = [alignment integerValue];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (VHLayoutParams *)generateDefaultLayoutParams {
|
||||||
|
return [[VHLayoutParams alloc] init];
|
||||||
|
}
|
||||||
|
|
||||||
- (void)measureByParent:(DoricGroupNode *)parent {
|
- (void)measureByParent:(DoricGroupNode *)parent {
|
||||||
DoricLayoutDesc widthSpec = self.layoutParams.width;
|
DoricLayoutDesc widthSpec = self.layoutParams.width;
|
||||||
DoricLayoutDesc heightSpec = self.layoutParams.height;
|
DoricLayoutDesc heightSpec = self.layoutParams.height;
|
||||||
CGFloat maxWidth = 0,maxHeight = 0;
|
CGFloat maxWidth = 0,maxHeight = 0;
|
||||||
for (DoricViewNode *child in self.indexedChildren) {
|
for (DoricViewNode *child in self.indexedChildren) {
|
||||||
[child measureByParent:self];
|
[child measureByParent:self];
|
||||||
UIView *childView = child.view;
|
CGFloat placeWidth = child.measuredWidth;
|
||||||
CGFloat placeWidth = childView.width + child.layoutParams.margin.left + child.layoutParams.margin.right;
|
CGFloat placeHeight = child.measuredHeight;
|
||||||
CGFloat placeHeight = childView.height + child.layoutParams.margin.top + child.layoutParams.margin.bottom;
|
|
||||||
maxWidth += placeWidth + self.space;
|
maxWidth += placeWidth + self.space;
|
||||||
maxHeight = MAX(maxHeight, placeHeight);
|
maxHeight = MAX(maxHeight, placeHeight);
|
||||||
}
|
}
|
||||||
@ -35,11 +69,61 @@ - (void)measureByParent:(DoricGroupNode *)parent {
|
|||||||
self.desiredHeight = maxHeight;
|
self.desiredHeight = maxHeight;
|
||||||
|
|
||||||
if (widthSpec == LAYOUT_WRAP_CONTENT) {
|
if (widthSpec == LAYOUT_WRAP_CONTENT) {
|
||||||
self.view.width = maxWidth;
|
self.width = maxWidth;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (heightSpec == LAYOUT_WRAP_CONTENT) {
|
if (heightSpec == LAYOUT_WRAP_CONTENT) {
|
||||||
self.view.height = maxHeight;
|
self.height = maxHeight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)layoutByParent:(DoricGroupNode *)parent {
|
||||||
|
if (self.layoutParams.width == LAYOUT_MATCH_PARENT) {
|
||||||
|
self.width = parent.width;
|
||||||
|
}
|
||||||
|
if (self.layoutParams.height == LAYOUT_MATCH_PARENT) {
|
||||||
|
self.height = parent.height;
|
||||||
|
}
|
||||||
|
// layotu child
|
||||||
|
CGFloat xStart = 0, yStart = 0;
|
||||||
|
if ((self.gravity & LEFT) == LEFT) {
|
||||||
|
xStart = 0;
|
||||||
|
} else if ((self.gravity & RIGHT) == RIGHT) {
|
||||||
|
xStart = self.width - self.desiredWidth;
|
||||||
|
} else if ((self.gravity & CENTER_X) == CENTER_X) {
|
||||||
|
xStart = (self.width -self.desiredWidth)/2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((self.gravity & TOP) == TOP) {
|
||||||
|
yStart = 0;
|
||||||
|
} else if ((self.gravity & BOTTOM) == BOTTOM) {
|
||||||
|
yStart = self.height - self.desiredHeight;
|
||||||
|
} else if ((self.gravity & CENTER_Y) == CENTER_Y) {
|
||||||
|
yStart = (self.height -self.desiredHeight)/2;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (DoricViewNode *child in self.indexedChildren) {
|
||||||
|
if (child.layoutParams.width == LAYOUT_MATCH_PARENT) {
|
||||||
|
child.width = self.width;
|
||||||
|
}
|
||||||
|
if (child.layoutParams.height == LAYOUT_MATCH_PARENT) {
|
||||||
|
child.height = self.height;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ([child.layoutParams isKindOfClass:VHLayoutParams.class]) {
|
||||||
|
VHLayoutParams *layoutParams = (VHLayoutParams *)child.layoutParams;
|
||||||
|
DoricGravity gravity = layoutParams.alignment;
|
||||||
|
if ((gravity & TOP) == TOP) {
|
||||||
|
child.top = yStart;
|
||||||
|
} else if ((gravity & BOTTOM) == BOTTOM) {
|
||||||
|
child.bottom = yStart + self.desiredHeight;
|
||||||
|
} else if ((gravity & CENTER_Y) == CENTER_Y) {
|
||||||
|
child.centerY = yStart + self.desiredHeight/2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
child.left = xStart;
|
||||||
|
xStart = child.right + self.space;
|
||||||
|
[child layoutByParent:self];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,12 +14,14 @@ - (void)setupRootView:(UIView *)view {
|
|||||||
|
|
||||||
- (void)measureByParent:(DoricGroupNode *)parent {
|
- (void)measureByParent:(DoricGroupNode *)parent {
|
||||||
// Do noting for root
|
// Do noting for root
|
||||||
|
[super measureByParent:self];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
- (void)render:(NSDictionary *)props {
|
- (void)render:(NSDictionary *)props {
|
||||||
[self blend:props];
|
[self blend:props];
|
||||||
[self measureByParent:self];
|
[self measureByParent:self];
|
||||||
|
[self layoutByParent:self];
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
NS_ASSUME_NONNULL_BEGIN
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
@interface DoricStackNode : DoricGroupNode
|
@interface DoricStackNode : DoricGroupNode<UIView *,StackLayoutParams *>
|
||||||
@property (nonatomic) DoricGravity gravity;
|
@property (nonatomic) DoricGravity gravity;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
@ -22,9 +22,8 @@ - (void)measureByParent:(DoricGroupNode *)parent {
|
|||||||
CGFloat maxWidth = 0,maxHeight = 0;
|
CGFloat maxWidth = 0,maxHeight = 0;
|
||||||
for (DoricViewNode *child in self.indexedChildren) {
|
for (DoricViewNode *child in self.indexedChildren) {
|
||||||
[child measureByParent:self];
|
[child measureByParent:self];
|
||||||
UIView *childView = child.view;
|
CGFloat placeWidth = child.measuredWidth;
|
||||||
CGFloat placeWidth = childView.width + child.layoutParams.margin.left + child.layoutParams.margin.right;
|
CGFloat placeHeight = child.measuredHeight;
|
||||||
CGFloat placeHeight = childView.height + child.layoutParams.margin.top + child.layoutParams.margin.bottom;
|
|
||||||
maxWidth = MAX(maxWidth, placeWidth);
|
maxWidth = MAX(maxWidth, placeWidth);
|
||||||
maxHeight = MAX(maxHeight, placeHeight);
|
maxHeight = MAX(maxHeight, placeHeight);
|
||||||
}
|
}
|
||||||
@ -32,43 +31,49 @@ - (void)measureByParent:(DoricGroupNode *)parent {
|
|||||||
self.desiredHeight = maxHeight;
|
self.desiredHeight = maxHeight;
|
||||||
|
|
||||||
if (widthSpec == LAYOUT_WRAP_CONTENT) {
|
if (widthSpec == LAYOUT_WRAP_CONTENT) {
|
||||||
self.view.width = maxWidth;
|
self.width = maxWidth;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (heightSpec == LAYOUT_WRAP_CONTENT) {
|
if (heightSpec == LAYOUT_WRAP_CONTENT) {
|
||||||
self.view.height = maxHeight;
|
self.height = maxHeight;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
- (LayoutParams *)generateDefaultLayoutParams {
|
||||||
|
return [[StackLayoutParams alloc] init];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)layoutByParent:(DoricGroupNode *)parent {
|
- (void)layoutByParent:(DoricGroupNode *)parent {
|
||||||
for (DoricViewNode *child in self.indexedChildren) {
|
for (DoricViewNode *child in self.indexedChildren) {
|
||||||
[child measureByParent:self];
|
[child measureByParent:self];
|
||||||
UIView *childView = child.view;
|
|
||||||
if (child.layoutParams.width == LAYOUT_MATCH_PARENT) {
|
if (child.layoutParams.width == LAYOUT_MATCH_PARENT) {
|
||||||
childView.width = self.view.width;
|
child.width = self.width;
|
||||||
}
|
}
|
||||||
if (child.layoutParams.height == LAYOUT_MATCH_PARENT) {
|
if (child.layoutParams.height == LAYOUT_MATCH_PARENT) {
|
||||||
childView.height = self.view.height;
|
child.height = self.height;
|
||||||
}
|
}
|
||||||
DoricGravity gravity = self.layoutParams.alignment;
|
DoricGravity gravity = self.gravity;
|
||||||
|
if ([child.layoutParams isKindOfClass:StackLayoutParams.class]) {
|
||||||
|
StackLayoutParams *layoutParams = (StackLayoutParams *)self.layoutParams;
|
||||||
|
gravity |= layoutParams.alignment;
|
||||||
|
}
|
||||||
|
|
||||||
if ((gravity & LEFT) == LEFT) {
|
if ((gravity & LEFT) == LEFT) {
|
||||||
childView.left = self.view.left;
|
child.left = self.left;
|
||||||
}
|
}
|
||||||
if ((gravity & RIGHT) == RIGHT) {
|
if ((gravity & RIGHT) == RIGHT) {
|
||||||
childView.right = self.view.right;
|
child.right = self.right;
|
||||||
}
|
}
|
||||||
if ((gravity & TOP) == TOP) {
|
if ((gravity & TOP) == TOP) {
|
||||||
childView.top = self.view.top;
|
child.top = self.top;
|
||||||
}
|
}
|
||||||
if ((gravity & BOTTOM) == BOTTOM) {
|
if ((gravity & BOTTOM) == BOTTOM) {
|
||||||
childView.bottom = self.view.bottom;
|
child.bottom = self.bottom;
|
||||||
}
|
}
|
||||||
if ((gravity & CENTER_X) == CENTER_X) {
|
if ((gravity & CENTER_X) == CENTER_X) {
|
||||||
childView.centerX = self.view.centerX;
|
child.centerX = self.centerX;
|
||||||
}
|
}
|
||||||
if ((gravity & CENTER_Y) == CENTER_Y) {
|
if ((gravity & CENTER_Y) == CENTER_Y) {
|
||||||
childView.centerY = self.view.centerY;
|
child.centerY = self.centerY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
NS_ASSUME_NONNULL_BEGIN
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
@interface DoricVLayoutNode : DoricGroupNode
|
@interface DoricVLayoutNode : DoricGroupNode<UIView *,VHLayoutParams *>
|
||||||
@property (nonatomic) CGFloat space;
|
@property (nonatomic) CGFloat space;
|
||||||
@property (nonatomic) DoricGravity gravity;
|
@property (nonatomic) DoricGravity gravity;
|
||||||
@end
|
@end
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#import "DoricVLayoutNode.h"
|
#import "DoricVLayoutNode.h"
|
||||||
|
#import "DoricUtil.h"
|
||||||
|
|
||||||
@implementation DoricVLayoutNode
|
@implementation DoricVLayoutNode
|
||||||
- (instancetype)init {
|
- (instancetype)init {
|
||||||
@ -16,15 +17,48 @@ - (instancetype)init {
|
|||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)blendView:(id)view forPropName:(NSString *)name propValue:(id)prop {
|
||||||
|
if ([name isEqualToString:@"gravity"]) {
|
||||||
|
self.gravity = [(NSNumber *)prop integerValue];
|
||||||
|
} else if ([name isEqualToString:@"space"]) {
|
||||||
|
self.space = [(NSNumber *)prop floatValue];
|
||||||
|
} else {
|
||||||
|
[super blendView:view forPropName:name propValue:prop];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)blendChild:(DoricViewNode *)child layoutConfig:(NSDictionary *)layoutconfig {
|
||||||
|
[super blendChild:child layoutConfig:layoutconfig];
|
||||||
|
if (![child.layoutParams isKindOfClass:VHLayoutParams.class]) {
|
||||||
|
DoricLog(@"blend VLayout child error,layout params not match");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
VHLayoutParams *params = (VHLayoutParams *)child.layoutParams;
|
||||||
|
NSDictionary *margin = [layoutconfig objectForKey:@"margin"];
|
||||||
|
if (margin) {
|
||||||
|
params.margin.top = [(NSNumber *)[margin objectForKey:@"top"] floatValue];
|
||||||
|
params.margin.left = [(NSNumber *)[margin objectForKey:@"left"] floatValue];
|
||||||
|
params.margin.right = [(NSNumber *)[margin objectForKey:@"right"] floatValue];
|
||||||
|
params.margin.bottom = [(NSNumber *)[margin objectForKey:@"bottom"] floatValue];
|
||||||
|
}
|
||||||
|
NSNumber *alignment = [layoutconfig objectForKey:@"alignment"];
|
||||||
|
if (alignment) {
|
||||||
|
params.alignment = [alignment integerValue];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (LayoutParams *)generateDefaultLayoutParams {
|
||||||
|
return [[VHLayoutParams alloc] init];
|
||||||
|
}
|
||||||
|
|
||||||
- (void)measureByParent:(DoricGroupNode *)parent {
|
- (void)measureByParent:(DoricGroupNode *)parent {
|
||||||
DoricLayoutDesc widthSpec = self.layoutParams.width;
|
DoricLayoutDesc widthSpec = self.layoutParams.width;
|
||||||
DoricLayoutDesc heightSpec = self.layoutParams.height;
|
DoricLayoutDesc heightSpec = self.layoutParams.height;
|
||||||
CGFloat maxWidth = 0,maxHeight = 0;
|
CGFloat maxWidth = 0,maxHeight = 0;
|
||||||
for (DoricViewNode *child in self.indexedChildren) {
|
for (DoricViewNode *child in self.indexedChildren) {
|
||||||
[child measureByParent:self];
|
[child measureByParent:self];
|
||||||
UIView *childView = child.view;
|
CGFloat placeWidth = child.measuredWidth;
|
||||||
CGFloat placeWidth = childView.width + child.layoutParams.margin.left + child.layoutParams.margin.right;
|
CGFloat placeHeight = child.measuredHeight;
|
||||||
CGFloat placeHeight = childView.height + child.layoutParams.margin.top + child.layoutParams.margin.bottom;
|
|
||||||
maxWidth = MAX(maxWidth, placeWidth);
|
maxWidth = MAX(maxWidth, placeWidth);
|
||||||
maxHeight += placeHeight + self.space;
|
maxHeight += placeHeight + self.space;
|
||||||
}
|
}
|
||||||
@ -34,52 +68,61 @@ - (void)measureByParent:(DoricGroupNode *)parent {
|
|||||||
self.desiredHeight = maxHeight;
|
self.desiredHeight = maxHeight;
|
||||||
|
|
||||||
if (widthSpec == LAYOUT_WRAP_CONTENT) {
|
if (widthSpec == LAYOUT_WRAP_CONTENT) {
|
||||||
self.view.width = maxWidth;
|
self.width = maxWidth;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (heightSpec == LAYOUT_WRAP_CONTENT) {
|
if (heightSpec == LAYOUT_WRAP_CONTENT) {
|
||||||
self.view.height = maxHeight;
|
self.height = maxHeight;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)layoutByParent:(DoricGroupNode *)parent {
|
- (void)layoutByParent:(DoricGroupNode *)parent {
|
||||||
if (self.layoutParams.width == LAYOUT_MATCH_PARENT) {
|
if (self.layoutParams.width == LAYOUT_MATCH_PARENT) {
|
||||||
self.view.width = parent.view.width;
|
self.width = parent.width;
|
||||||
}
|
}
|
||||||
if (self.layoutParams.height == LAYOUT_MATCH_PARENT) {
|
if (self.layoutParams.height == LAYOUT_MATCH_PARENT) {
|
||||||
self.view.height = parent.view.height;
|
self.height = parent.height;
|
||||||
}
|
}
|
||||||
CGFloat start = 0;
|
// layotu child
|
||||||
|
CGFloat xStart = 0, yStart = 0;
|
||||||
|
if ((self.gravity & LEFT) == LEFT) {
|
||||||
|
xStart = 0;
|
||||||
|
} else if ((self.gravity & RIGHT) == RIGHT) {
|
||||||
|
xStart = self.width - self.desiredWidth;
|
||||||
|
} else if ((self.gravity & CENTER_X) == CENTER_X) {
|
||||||
|
xStart = (self.width -self.desiredWidth)/2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((self.gravity & TOP) == TOP) {
|
||||||
|
yStart = 0;
|
||||||
|
} else if ((self.gravity & BOTTOM) == BOTTOM) {
|
||||||
|
yStart = self.height - self.desiredHeight;
|
||||||
|
} else if ((self.gravity & CENTER_Y) == CENTER_Y) {
|
||||||
|
yStart = (self.height -self.desiredHeight)/2;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
for (DoricViewNode *child in self.indexedChildren) {
|
for (DoricViewNode *child in self.indexedChildren) {
|
||||||
[child measureByParent:self];
|
|
||||||
UIView *childView = child.view;
|
|
||||||
if (child.layoutParams.width == LAYOUT_MATCH_PARENT) {
|
if (child.layoutParams.width == LAYOUT_MATCH_PARENT) {
|
||||||
childView.width = self.view.width;
|
child.width = self.width;
|
||||||
}
|
}
|
||||||
if (child.layoutParams.height == LAYOUT_MATCH_PARENT) {
|
if (child.layoutParams.height == LAYOUT_MATCH_PARENT) {
|
||||||
childView.height = self.view.height;
|
child.height = self.height;
|
||||||
}
|
}
|
||||||
DoricGravity gravity = self.layoutParams.alignment;
|
if ([child.layoutParams isKindOfClass:VHLayoutParams.class]) {
|
||||||
|
VHLayoutParams *layoutParams = (VHLayoutParams *)child.layoutParams;
|
||||||
|
DoricGravity gravity = layoutParams.alignment;
|
||||||
if ((gravity & LEFT) == LEFT) {
|
if ((gravity & LEFT) == LEFT) {
|
||||||
childView.left = self.view.left;
|
child.left = xStart;
|
||||||
|
} else if ((gravity & RIGHT) == RIGHT) {
|
||||||
|
child.right = xStart + self.desiredWidth;
|
||||||
|
} else if ((gravity & CENTER_X) == CENTER_X) {
|
||||||
|
child.centerX = xStart + self.desiredWidth/2;
|
||||||
}
|
}
|
||||||
if ((gravity & RIGHT) == RIGHT) {
|
|
||||||
childView.right = self.view.right;
|
|
||||||
}
|
}
|
||||||
if ((gravity & TOP) == TOP) {
|
child.top = yStart;
|
||||||
childView.top = self.view.top;
|
yStart = child.bottom + self.space;
|
||||||
}
|
[child layoutByParent:self];
|
||||||
if ((gravity & BOTTOM) == BOTTOM) {
|
|
||||||
childView.bottom = self.view.bottom;
|
|
||||||
}
|
|
||||||
if ((gravity & CENTER_X) == CENTER_X) {
|
|
||||||
childView.centerX = self.view.centerX;
|
|
||||||
}
|
|
||||||
if ((gravity & CENTER_Y) == CENTER_Y) {
|
|
||||||
childView.centerY = self.view.centerY;
|
|
||||||
}
|
|
||||||
childView.top = start;
|
|
||||||
start = childView.bottom + self.space;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@end
|
@end
|
||||||
|
@ -23,7 +23,7 @@ typedef NS_ENUM(NSInteger,DoricGravity) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
typedef NS_ENUM(NSInteger,DoricLayoutDesc) {
|
typedef NS_ENUM(NSInteger,DoricLayoutDesc) {
|
||||||
LAYOUT_DEFAULT = 0,
|
LAYOUT_ABSOLUTE = 0,
|
||||||
LAYOUT_MATCH_PARENT = -1,
|
LAYOUT_MATCH_PARENT = -1,
|
||||||
LAYOUT_WRAP_CONTENT = -2,
|
LAYOUT_WRAP_CONTENT = -2,
|
||||||
};
|
};
|
||||||
@ -42,10 +42,22 @@ NS_ASSUME_NONNULL_BEGIN
|
|||||||
@interface LayoutParams : NSObject
|
@interface LayoutParams : NSObject
|
||||||
@property (nonatomic) DoricLayoutDesc width;
|
@property (nonatomic) DoricLayoutDesc width;
|
||||||
@property (nonatomic) DoricLayoutDesc height;
|
@property (nonatomic) DoricLayoutDesc height;
|
||||||
@property (nonatomic) DoricGravity alignment;
|
@end
|
||||||
|
|
||||||
|
@interface MarginLayoutParams : LayoutParams
|
||||||
@property (nonatomic,strong) DoricRect *margin;
|
@property (nonatomic,strong) DoricRect *margin;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
@interface StackLayoutParams : LayoutParams
|
||||||
|
@property (nonatomic) DoricGravity alignment;
|
||||||
|
@end
|
||||||
|
|
||||||
|
@interface VHLayoutParams : MarginLayoutParams
|
||||||
|
@property (nonatomic) DoricGravity alignment;
|
||||||
|
@property (nonatomic) NSInteger weight;
|
||||||
|
@end
|
||||||
|
|
||||||
|
|
||||||
@interface UIView(DoricContainer)
|
@interface UIView(DoricContainer)
|
||||||
|
|
||||||
@property (nonatomic,strong) LayoutParams *layoutParams;
|
@property (nonatomic,strong) LayoutParams *layoutParams;
|
||||||
|
@ -21,14 +21,37 @@ - (instancetype)init {
|
|||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation LayoutParams
|
@implementation LayoutParams
|
||||||
|
- (instancetype)init {
|
||||||
|
if (self = [super init]) {
|
||||||
|
_width = LAYOUT_WRAP_CONTENT;
|
||||||
|
_height = LAYOUT_WRAP_CONTENT;
|
||||||
|
}
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
@end
|
||||||
|
@implementation MarginLayoutParams
|
||||||
- (instancetype)init {
|
- (instancetype)init {
|
||||||
if (self = [super init]) {
|
if (self = [super init]) {
|
||||||
_margin = [[DoricRect alloc] init];
|
_margin = [[DoricRect alloc] init];
|
||||||
_width = LAYOUT_DEFAULT;
|
}
|
||||||
_height = LAYOUT_DEFAULT;
|
return self;
|
||||||
|
}@end
|
||||||
|
|
||||||
|
@implementation StackLayoutParams
|
||||||
|
- (instancetype)init {
|
||||||
|
if (self = [super init]) {
|
||||||
_alignment = 0;
|
_alignment = 0;
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
|
}@end
|
||||||
|
|
||||||
|
@implementation VHLayoutParams
|
||||||
|
- (instancetype)init {
|
||||||
|
if (self = [super init]) {
|
||||||
|
_alignment = 0;
|
||||||
|
_weight = 0;
|
||||||
|
}
|
||||||
|
return self;
|
||||||
}
|
}
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
NS_ASSUME_NONNULL_BEGIN
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
@class DoricGroupNode;
|
@class DoricGroupNode;
|
||||||
|
|
||||||
@interface DoricViewNode <V>: DoricContextHolder
|
@interface DoricViewNode <V:UIView *>: DoricContextHolder
|
||||||
|
|
||||||
@property (nonatomic,strong) V view;
|
@property (nonatomic,strong) V view;
|
||||||
|
|
||||||
@ -37,6 +37,8 @@ NS_ASSUME_NONNULL_BEGIN
|
|||||||
@property (nonatomic) CGFloat left;
|
@property (nonatomic) CGFloat left;
|
||||||
@property (nonatomic) CGFloat right;
|
@property (nonatomic) CGFloat right;
|
||||||
@property (nonatomic) CGFloat bottom;
|
@property (nonatomic) CGFloat bottom;
|
||||||
|
@property (nonatomic,readonly) CGFloat measuredWidth;
|
||||||
|
@property (nonatomic,readonly) CGFloat measuredHeight;
|
||||||
|
|
||||||
- (V)build:(NSDictionary *)props;
|
- (V)build:(NSDictionary *)props;
|
||||||
|
|
||||||
@ -51,7 +53,6 @@ NS_ASSUME_NONNULL_BEGIN
|
|||||||
- (void)layoutByParent:(DoricGroupNode *)parent;
|
- (void)layoutByParent:(DoricGroupNode *)parent;
|
||||||
|
|
||||||
+ (DoricViewNode *)create:(DoricContext *)context withType:(NSString *)type;
|
+ (DoricViewNode *)create:(DoricContext *)context withType:(NSString *)type;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_END
|
NS_ASSUME_NONNULL_END
|
||||||
|
@ -33,6 +33,7 @@ - (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop
|
|||||||
if ([width integerValue] < 0) {
|
if ([width integerValue] < 0) {
|
||||||
self.layoutParams.width = [width integerValue];
|
self.layoutParams.width = [width integerValue];
|
||||||
} else {
|
} else {
|
||||||
|
self.layoutParams.width = LAYOUT_ABSOLUTE;
|
||||||
view.width = [width floatValue];
|
view.width = [width floatValue];
|
||||||
}
|
}
|
||||||
} else if([name isEqualToString:@"height"]) {
|
} else if([name isEqualToString:@"height"]) {
|
||||||
@ -40,6 +41,7 @@ - (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop
|
|||||||
if ([height integerValue] < 0) {
|
if ([height integerValue] < 0) {
|
||||||
self.layoutParams.height = [height integerValue];
|
self.layoutParams.height = [height integerValue];
|
||||||
} else {
|
} else {
|
||||||
|
self.layoutParams.height = LAYOUT_ABSOLUTE;
|
||||||
view.height = [height floatValue];
|
view.height = [height floatValue];
|
||||||
}
|
}
|
||||||
} else if([name isEqualToString:@"x"]) {
|
} else if([name isEqualToString:@"x"]) {
|
||||||
@ -57,6 +59,22 @@ - (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (CGFloat)measuredWidth {
|
||||||
|
if ([self.layoutParams isKindOfClass: MarginLayoutParams.class]) {
|
||||||
|
MarginLayoutParams *marginParams = (MarginLayoutParams *)self.layoutParams;
|
||||||
|
return self.width + marginParams.margin.left + marginParams.margin.right;
|
||||||
|
}
|
||||||
|
return self.width;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (CGFloat)measuredHeight {
|
||||||
|
if ([self.layoutParams isKindOfClass: MarginLayoutParams.class]) {
|
||||||
|
MarginLayoutParams *marginParams = (MarginLayoutParams *)self.layoutParams;
|
||||||
|
return self.height + marginParams.margin.top + marginParams.margin.bottom;
|
||||||
|
}
|
||||||
|
return self.height;
|
||||||
|
}
|
||||||
|
|
||||||
- (void)measureByParent:(DoricGroupNode *)parent {
|
- (void)measureByParent:(DoricGroupNode *)parent {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,9 +17,9 @@ void DoricLog(NSString * _Nonnull format, ...) {
|
|||||||
UIColor *DoricColor(NSNumber *number) {
|
UIColor *DoricColor(NSNumber *number) {
|
||||||
CGFloat r, g, b, a;
|
CGFloat r, g, b, a;
|
||||||
long colorValue = [number longValue];
|
long colorValue = [number longValue];
|
||||||
a = ((colorValue >> 6) & 0xff)/225.0f;
|
a = ((colorValue >> 24) & 0xff)/255.0f;
|
||||||
r = ((colorValue >> 4) & 0xff)/225.0f;
|
r = ((colorValue >> 16) & 0xff)/255.0f;
|
||||||
g = ((colorValue >> 2) & 0xff)/225.0f;
|
g = ((colorValue >> 8) & 0xff)/255.0f;
|
||||||
b = ((colorValue >> 0) & 0xff)/225.0f;
|
b = ((colorValue >> 0) & 0xff)/255.0f;
|
||||||
return [UIColor colorWithRed:r green:g blue:b alpha:a];
|
return [UIColor colorWithRed:r green:g blue:b alpha:a];
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user