iOS:Gravity and Margin shold affect at the same time

This commit is contained in:
pengfei.zhou 2020-03-21 16:03:03 +08:00 committed by osborn
parent f58d9dbcf4
commit 201278629a
2 changed files with 40 additions and 18 deletions

View File

@ -260,10 +260,8 @@ - (void)layoutSelf:(CGSize)targetSize {
} else if ((gravity & CENTER_X) == CENTER_X) { } else if ((gravity & CENTER_X) == CENTER_X) {
child.centerX = targetSize.width / 2; child.centerX = targetSize.width / 2;
} else { } else {
if (childConfig.margin.left) { if (childConfig.margin.left || childConfig.margin.right) {
child.left = childConfig.margin.left + padding.left; child.left = padding.left;
} else if (childConfig.margin.right) {
child.right = targetSize.width - childConfig.margin.right - padding.right;
} }
} }
if ((gravity & TOP) == TOP) { if ((gravity & TOP) == TOP) {
@ -273,12 +271,26 @@ - (void)layoutSelf:(CGSize)targetSize {
} else if ((gravity & CENTER_Y) == CENTER_Y) { } else if ((gravity & CENTER_Y) == CENTER_Y) {
child.centerY = targetSize.height / 2; child.centerY = targetSize.height / 2;
} else { } else {
if (childConfig.margin.top) { if (childConfig.margin.top || childConfig.margin.bottom) {
child.top = childConfig.margin.top + padding.top; child.top = padding.top;
} else if (childConfig.margin.bottom) {
child.bottom = targetSize.height - childConfig.margin.bottom - padding.bottom;
} }
} }
if (!gravity) {
gravity = LEFT | TOP;
}
if (childConfig.margin.left && !((gravity & RIGHT) == RIGHT)) {
child.left += childConfig.margin.left;
}
if (childConfig.margin.right && !((gravity & LEFT) == LEFT)) {
child.right -= childConfig.margin.right;
}
if (childConfig.margin.top && !((gravity & BOTTOM) == BOTTOM)) {
child.top += childConfig.margin.top;
}
if (childConfig.margin.bottom && !((gravity & TOP) == TOP)) {
child.bottom -= childConfig.margin.bottom;
}
} }
} }
@end @end
@ -359,13 +371,18 @@ - (void)layoutSelf:(CGSize)targetSize {
child.right = self.width - padding.right; child.right = self.width - padding.right;
} else if ((gravity & CENTER_X) == CENTER_X) { } else if ((gravity & CENTER_X) == CENTER_X) {
child.centerX = targetSize.width / 2; child.centerX = targetSize.width / 2;
} else if (childConfig.margin.left) {
child.left = childConfig.margin.left + padding.left;
} else if (childConfig.margin.right) {
child.right = targetSize.width - childConfig.margin.right - padding.right;
} else { } else {
child.left = padding.left; child.left = padding.left;
} }
if (!gravity) {
gravity = LEFT;
}
if (childConfig.margin.left && !((gravity & RIGHT) == RIGHT)) {
child.left += childConfig.margin.left;
}
if (childConfig.margin.right && !((gravity & LEFT) == LEFT)) {
child.right -= childConfig.margin.right;
}
if (childConfig.margin.top) { if (childConfig.margin.top) {
yStart += childConfig.margin.top; yStart += childConfig.margin.top;
} }
@ -454,13 +471,18 @@ - (void)layoutSelf:(CGSize)targetSize {
child.bottom = targetSize.height - padding.bottom; child.bottom = targetSize.height - padding.bottom;
} else if ((gravity & CENTER_Y) == CENTER_Y) { } else if ((gravity & CENTER_Y) == CENTER_Y) {
child.centerY = targetSize.height / 2; child.centerY = targetSize.height / 2;
} else if (childConfig.margin.top) {
child.top = childConfig.margin.top + padding.top;
} else if (childConfig.margin.bottom) {
child.bottom = targetSize.height - childConfig.margin.bottom - padding.bottom;
} else { } else {
child.top = padding.top; child.top = padding.top;
} }
if (!gravity) {
gravity = TOP;
}
if (childConfig.margin.top && !((gravity & BOTTOM) == BOTTOM)) {
child.top += childConfig.margin.top;
}
if (childConfig.margin.bottom && !((gravity & TOP) == TOP)) {
child.bottom -= childConfig.margin.bottom;
}
if (childConfig.margin.left) { if (childConfig.margin.left) {
xStart += childConfig.margin.left; xStart += childConfig.margin.left;

View File

@ -182,9 +182,9 @@ - (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop
self.gradientLayer.frame = view.bounds; self.gradientLayer.frame = view.bounds;
} }
} else if ([name isEqualToString:@"x"]) { } else if ([name isEqualToString:@"x"]) {
view.x = [(NSNumber *) prop floatValue]; view.x = [prop floatValue];
} else if ([name isEqualToString:@"y"]) { } else if ([name isEqualToString:@"y"]) {
view.y = [(NSNumber *) prop floatValue]; view.y = [prop floatValue];
} else if ([name isEqualToString:@"backgroundColor"]) { } else if ([name isEqualToString:@"backgroundColor"]) {
if ([prop isKindOfClass:[NSNumber class]]) { if ([prop isKindOfClass:[NSNumber class]]) {
view.backgroundColor = DoricColor(prop); view.backgroundColor = DoricColor(prop);