iOS render stack and soon

This commit is contained in:
pengfei.zhou
2019-07-31 17:43:26 +08:00
parent 674335324b
commit b0e34a316b
14 changed files with 284 additions and 85 deletions

View File

@@ -6,6 +6,7 @@
//
#import "DoricVLayoutNode.h"
#import "DoricUtil.h"
@implementation DoricVLayoutNode
- (instancetype)init {
@@ -16,15 +17,48 @@ - (instancetype)init {
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 {
DoricLayoutDesc widthSpec = self.layoutParams.width;
DoricLayoutDesc heightSpec = self.layoutParams.height;
CGFloat maxWidth = 0,maxHeight = 0;
for (DoricViewNode *child in self.indexedChildren) {
[child measureByParent:self];
UIView *childView = child.view;
CGFloat placeWidth = childView.width + child.layoutParams.margin.left + child.layoutParams.margin.right;
CGFloat placeHeight = childView.height + child.layoutParams.margin.top + child.layoutParams.margin.bottom;
CGFloat placeWidth = child.measuredWidth;
CGFloat placeHeight = child.measuredHeight;
maxWidth = MAX(maxWidth, placeWidth);
maxHeight += placeHeight + self.space;
}
@@ -34,52 +68,61 @@ - (void)measureByParent:(DoricGroupNode *)parent {
self.desiredHeight = maxHeight;
if (widthSpec == LAYOUT_WRAP_CONTENT) {
self.view.width = maxWidth;
self.width = maxWidth;
}
if (heightSpec == LAYOUT_WRAP_CONTENT) {
self.view.height = maxHeight;
self.height = maxHeight;
}
}
- (void)layoutByParent:(DoricGroupNode *)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) {
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) {
[child measureByParent:self];
UIView *childView = child.view;
if (child.layoutParams.width == LAYOUT_MATCH_PARENT) {
childView.width = self.view.width;
child.width = self.width;
}
if (child.layoutParams.height == LAYOUT_MATCH_PARENT) {
childView.height = self.view.height;
child.height = self.height;
}
DoricGravity gravity = self.layoutParams.alignment;
if ((gravity & LEFT) == LEFT) {
childView.left = self.view.left;
}
if ((gravity & RIGHT) == RIGHT) {
childView.right = self.view.right;
}
if ((gravity & TOP) == TOP) {
childView.top = self.view.top;
}
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;
if ([child.layoutParams isKindOfClass:VHLayoutParams.class]) {
VHLayoutParams *layoutParams = (VHLayoutParams *)child.layoutParams;
DoricGravity gravity = layoutParams.alignment;
if ((gravity & LEFT) == 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;
}
}
child.top = yStart;
yStart = child.bottom + self.space;
[child layoutByParent:self];
}
}
@end