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

@ -9,7 +9,7 @@
NS_ASSUME_NONNULL_BEGIN
@interface DoricGroupNode : DoricViewNode<UIView *>
@interface DoricGroupNode <V:UIView *,P:LayoutParams *>: DoricViewNode<V>
@property (nonatomic,strong) NSMutableDictionary *children;
@property (nonatomic,strong) NSMutableArray *indexedChildren;
@ -19,7 +19,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)blendChild:(DoricViewNode *)child layoutConfig:(NSDictionary *)layoutconfig;
- (LayoutParams *)generateDefaultLayoutParams;
- (P)generateDefaultLayoutParams;
@end
NS_ASSUME_NONNULL_END

View File

@ -9,14 +9,13 @@
@implementation DoricGroupNode
- (instancetype)init {
if(self = [super init]) {
- (instancetype)initWithContext:(DoricContext *)doricContext {
if(self = [super initWithContext:doricContext]) {
_children = [[NSMutableDictionary alloc] init];
_indexedChildren = [[NSMutableArray alloc] init];
}
return self;
}
- (UIView *)build:(NSDictionary *)props {
return [[UIView alloc] init];
}
@ -40,26 +39,34 @@ - (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop
node.viewId = viewId;
[self.children setObject:node forKey:viewId];
} else if (i != node.index){
[self.indexedChildren removeObjectAtIndex:i];
node.index = i;
[node.view removeFromSuperview];
}
LayoutParams *params = node.layoutParams;
if (params == nil) {
params = [self generateDefaultLayoutParams];
node.layoutParams = params;
}
[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.indexedChildren setObject:node atIndexedSubscript:i];
}
}
while (i < self.view.subviews.count) {
[self.view.subviews[i] removeFromSuperview];
DoricViewNode *node = [self.indexedChildren objectAtIndex:i];
if (node != nil) {
[self.children removeObjectForKey: node.viewId];
[self.indexedChildren removeObjectAtIndex:i];
UIView *view = self.view.subviews[i];
if (i < self.indexedChildren.count) {
DoricViewNode *node = [self.indexedChildren objectAtIndex:i];
if (node && node.view == view) {
[self.children removeObjectForKey: node.viewId];
[self.indexedChildren removeObjectAtIndex:i];
[view removeFromSuperview];
}
}
i++;
}
@ -75,13 +82,17 @@ - (LayoutParams *)generateDefaultLayoutParams {
- (void)blendChild:(DoricViewNode *)child layoutConfig:(NSDictionary *)layoutconfig {
LayoutParams *params = 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];
if ([params isKindOfClass:MarginLayoutParams.class]) {
MarginLayoutParams *marginParams = (MarginLayoutParams *)params;
NSDictionary *margin = [layoutconfig objectForKey:@"margin"];
if (margin) {
marginParams.margin.top = [(NSNumber *)[margin objectForKey:@"top"] floatValue];
marginParams.margin.left = [(NSNumber *)[margin objectForKey:@"left"] floatValue];
marginParams.margin.right = [(NSNumber *)[margin objectForKey:@"right"] floatValue];
marginParams.margin.bottom = [(NSNumber *)[margin objectForKey:@"bottom"] floatValue];
}
}
}
@end

View File

@ -9,7 +9,7 @@
NS_ASSUME_NONNULL_BEGIN
@interface DoricHLayoutNode : DoricGroupNode
@interface DoricHLayoutNode : DoricGroupNode<UIView *,VHLayoutParams *>
@property (nonatomic) CGFloat space;
@property (nonatomic) DoricGravity gravity;
@end

View File

@ -6,6 +6,7 @@
//
#import "DoricHLayoutNode.h"
#import "DoricUtil.h"
@implementation DoricHLayoutNode
- (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];
}
}
- (VHLayoutParams *)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 += placeWidth + self.space;
maxHeight = MAX(maxHeight, placeHeight);
}
@ -35,11 +69,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.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];
}
}

View File

@ -14,12 +14,14 @@ - (void)setupRootView:(UIView *)view {
- (void)measureByParent:(DoricGroupNode *)parent {
// Do noting for root
[super measureByParent:self];
}
- (void)render:(NSDictionary *)props {
[self blend:props];
[self measureByParent:self];
[self layoutByParent:self];
}
@end

View File

@ -9,7 +9,7 @@
NS_ASSUME_NONNULL_BEGIN
@interface DoricStackNode : DoricGroupNode
@interface DoricStackNode : DoricGroupNode<UIView *,StackLayoutParams *>
@property (nonatomic) DoricGravity gravity;
@end

View File

@ -22,9 +22,8 @@ - (void)measureByParent:(DoricGroupNode *)parent {
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 = MAX(maxHeight, placeHeight);
}
@ -32,43 +31,49 @@ - (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;
}
}
- (LayoutParams *)generateDefaultLayoutParams {
return [[StackLayoutParams alloc] init];
}
- (void)layoutByParent:(DoricGroupNode *)parent {
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;
DoricGravity gravity = self.gravity;
if ([child.layoutParams isKindOfClass:StackLayoutParams.class]) {
StackLayoutParams *layoutParams = (StackLayoutParams *)self.layoutParams;
gravity |= layoutParams.alignment;
}
if ((gravity & LEFT) == LEFT) {
childView.left = self.view.left;
child.left = self.left;
}
if ((gravity & RIGHT) == RIGHT) {
childView.right = self.view.right;
child.right = self.right;
}
if ((gravity & TOP) == TOP) {
childView.top = self.view.top;
child.top = self.top;
}
if ((gravity & BOTTOM) == BOTTOM) {
childView.bottom = self.view.bottom;
child.bottom = self.bottom;
}
if ((gravity & CENTER_X) == CENTER_X) {
childView.centerX = self.view.centerX;
child.centerX = self.centerX;
}
if ((gravity & CENTER_Y) == CENTER_Y) {
childView.centerY = self.view.centerY;
child.centerY = self.centerY;
}
}
}

View File

@ -9,7 +9,7 @@
NS_ASSUME_NONNULL_BEGIN
@interface DoricVLayoutNode : DoricGroupNode
@interface DoricVLayoutNode : DoricGroupNode<UIView *,VHLayoutParams *>
@property (nonatomic) CGFloat space;
@property (nonatomic) DoricGravity gravity;
@end

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

View File

@ -23,7 +23,7 @@ typedef NS_ENUM(NSInteger,DoricGravity) {
};
typedef NS_ENUM(NSInteger,DoricLayoutDesc) {
LAYOUT_DEFAULT = 0,
LAYOUT_ABSOLUTE = 0,
LAYOUT_MATCH_PARENT = -1,
LAYOUT_WRAP_CONTENT = -2,
};
@ -42,10 +42,22 @@ NS_ASSUME_NONNULL_BEGIN
@interface LayoutParams : NSObject
@property (nonatomic) DoricLayoutDesc width;
@property (nonatomic) DoricLayoutDesc height;
@property (nonatomic) DoricGravity alignment;
@end
@interface MarginLayoutParams : LayoutParams
@property (nonatomic,strong) DoricRect *margin;
@end
@interface StackLayoutParams : LayoutParams
@property (nonatomic) DoricGravity alignment;
@end
@interface VHLayoutParams : MarginLayoutParams
@property (nonatomic) DoricGravity alignment;
@property (nonatomic) NSInteger weight;
@end
@interface UIView(DoricContainer)
@property (nonatomic,strong) LayoutParams *layoutParams;

View File

@ -21,14 +21,37 @@ - (instancetype)init {
@end
@implementation LayoutParams
- (instancetype)init {
if (self = [super init]) {
_width = LAYOUT_WRAP_CONTENT;
_height = LAYOUT_WRAP_CONTENT;
}
return self;
}
@end
@implementation MarginLayoutParams
- (instancetype)init {
if (self = [super init]) {
_margin = [[DoricRect alloc] init];
_width = LAYOUT_DEFAULT;
_height = LAYOUT_DEFAULT;
}
return self;
}@end
@implementation StackLayoutParams
- (instancetype)init {
if (self = [super init]) {
_alignment = 0;
}
return self;
}@end
@implementation VHLayoutParams
- (instancetype)init {
if (self = [super init]) {
_alignment = 0;
_weight = 0;
}
return self;
}
@end

View File

@ -13,7 +13,7 @@
NS_ASSUME_NONNULL_BEGIN
@class DoricGroupNode;
@interface DoricViewNode <V>: DoricContextHolder
@interface DoricViewNode <V:UIView *>: DoricContextHolder
@property (nonatomic,strong) V view;
@ -37,6 +37,8 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic) CGFloat left;
@property (nonatomic) CGFloat right;
@property (nonatomic) CGFloat bottom;
@property (nonatomic,readonly) CGFloat measuredWidth;
@property (nonatomic,readonly) CGFloat measuredHeight;
- (V)build:(NSDictionary *)props;
@ -51,7 +53,6 @@ NS_ASSUME_NONNULL_BEGIN
- (void)layoutByParent:(DoricGroupNode *)parent;
+ (DoricViewNode *)create:(DoricContext *)context withType:(NSString *)type;
@end
NS_ASSUME_NONNULL_END

View File

@ -33,6 +33,7 @@ - (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop
if ([width integerValue] < 0) {
self.layoutParams.width = [width integerValue];
} else {
self.layoutParams.width = LAYOUT_ABSOLUTE;
view.width = [width floatValue];
}
} else if([name isEqualToString:@"height"]) {
@ -40,6 +41,7 @@ - (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop
if ([height integerValue] < 0) {
self.layoutParams.height = [height integerValue];
} else {
self.layoutParams.height = LAYOUT_ABSOLUTE;
view.height = [height floatValue];
}
} 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 {
}

View File

@ -17,9 +17,9 @@ void DoricLog(NSString * _Nonnull format, ...) {
UIColor *DoricColor(NSNumber *number) {
CGFloat r, g, b, a;
long colorValue = [number longValue];
a = ((colorValue >> 6) & 0xff)/225.0f;
r = ((colorValue >> 4) & 0xff)/225.0f;
g = ((colorValue >> 2) & 0xff)/225.0f;
b = ((colorValue >> 0) & 0xff)/225.0f;
a = ((colorValue >> 24) & 0xff)/255.0f;
r = ((colorValue >> 16) & 0xff)/255.0f;
g = ((colorValue >> 8) & 0xff)/255.0f;
b = ((colorValue >> 0) & 0xff)/255.0f;
return [UIColor colorWithRed:r green:g blue:b alpha:a];
}