iOS: DoricLayout fix code error

This commit is contained in:
pengfei.zhou 2022-08-24 19:09:49 +08:00 committed by osborn
parent 7749ea2ea2
commit 8fbbaa7f56

View File

@ -130,6 +130,7 @@ @interface DoricLayout ()
* 0xff--ff * 0xff--ff
* */ * */
@property(nonatomic, assign) NSUInteger measuredState; @property(nonatomic, assign) NSUInteger measuredState;
@property(nonatomic, strong) NSMutableDictionary *measuredCache;
@end @end
@implementation DoricLayout @implementation DoricLayout
@ -142,6 +143,7 @@ - (instancetype)init {
_minWidth = -1; _minWidth = -1;
_minHeight = -1; _minHeight = -1;
_measuredState = 0; _measuredState = 0;
_measuredCache = [NSMutableDictionary new];
} }
return self; return self;
} }
@ -166,11 +168,36 @@ - (void)apply {
} }
- (void)measure:(CGSize)targetSize { - (void)measure:(CGSize)targetSize {
[self measureWidth:DoricMeasureSpecMake(DoricMeasureExactly, targetSize.width) [self doMeasure:targetSize];
height:DoricMeasureSpecMake(DoricMeasureExactly, targetSize.height)];
[self layout]; [self layout];
} }
- (DoricMeasureSpec)getRootMeasureSpec:(CGFloat)targetSize
layoutSpec:(DoricLayoutSpec)spec
size:(CGFloat)size {
switch (spec) {
case DoricLayoutMost:
return DoricMeasureSpecMake(DoricMeasureExactly, targetSize);
case DoricLayoutFit:
return DoricMeasureSpecMake(DoricMeasureAtMost, targetSize);
default:
return DoricMeasureSpecMake(DoricMeasureExactly, size);
}
}
- (void)doMeasure:(CGSize)targetSize {
DoricMeasureSpec widthSpec = [self getRootMeasureSpec:targetSize.width
layoutSpec:self.widthSpec
size:self.width];
DoricMeasureSpec heightSpec = [self getRootMeasureSpec:targetSize.height
layoutSpec:self.heightSpec
size:self.height];
[self measureWidth:widthSpec
height:heightSpec];
}
#pragma helper #pragma helper
- (CGFloat)takenWidth { - (CGFloat)takenWidth {
@ -424,6 +451,20 @@ - (void)forceUniformHeight:(DoricMeasureSpec)widthMeasureSpec {
} }
- (void)measureWidth:(DoricMeasureSpec)widthSpec height:(DoricMeasureSpec)heightSpec { - (void)measureWidth:(DoricMeasureSpec)widthSpec height:(DoricMeasureSpec)heightSpec {
// NSString *measuredKey = [NSString stringWithFormat:@"%@;%@;%@;%@",
// @(widthSpec.mode), @(widthSpec.size),
// @(heightSpec.mode), @(heightSpec.size)];
//
// NSString *cached = self.measuredCache[measuredKey];
// if (cached) {
// NSArray <NSString *> *nums = [cached componentsSeparatedByString:@":"];
// if (nums.count == 2) {
// self.measuredWidth = nums[0].floatValue;
// self.measuredHeight = nums[1].floatValue;
// return;
// }
// }
switch (self.layoutType) { switch (self.layoutType) {
case DoricStack: { case DoricStack: {
[self stackMeasureWidth:widthSpec height:heightSpec]; [self stackMeasureWidth:widthSpec height:heightSpec];
@ -442,6 +483,10 @@ - (void)measureWidth:(DoricMeasureSpec)widthSpec height:(DoricMeasureSpec)height
break; break;
} }
} }
// self.measuredCache[measuredKey] = [NSString stringWithFormat:@"%@;%@",
// @(self.measuredWidth),
// @(self.measuredHeight)];
} }
- (void)verticalMeasureWidth:(DoricMeasureSpec)widthMeasureSpec - (void)verticalMeasureWidth:(DoricMeasureSpec)widthMeasureSpec
@ -734,7 +779,7 @@ - (void)horizontalMeasureWidth:(DoricMeasureSpec)widthMeasureSpec
childLayout.width = oldWidth; childLayout.width = oldWidth;
} }
CGFloat childWidth = childLayout.measuredHeight; CGFloat childWidth = childLayout.measuredWidth;
if (isExactly) { if (isExactly) {
totalLength += childWidth + childLayout.marginLeft + childLayout.marginRight; totalLength += childWidth + childLayout.marginLeft + childLayout.marginRight;
} else { } else {
@ -986,13 +1031,16 @@ - (void)stackMeasureWidth:(DoricMeasureSpec)widthMeasureSpec
- (void)undefinedMeasureWidth:(DoricMeasureSpec)widthMeasureSpec - (void)undefinedMeasureWidth:(DoricMeasureSpec)widthMeasureSpec
height:(DoricMeasureSpec)heightMeasureSpec { height:(DoricMeasureSpec)heightMeasureSpec {
CGSize targetSize = CGSizeMake(widthMeasureSpec.size, heightMeasureSpec.size); CGSize targetSize = CGSizeMake(widthMeasureSpec.size - self.paddingLeft - self.paddingRight,
heightMeasureSpec.size - self.paddingTop - self.paddingBottom);
//TODO should check this //TODO should check this
CGSize measuredSize = [self.view sizeThatFits:targetSize]; CGSize measuredSize = [self.view sizeThatFits:targetSize];
DoricSizeAndState widthSizeAndState = [self resolveSizeAndState:measuredSize.width DoricSizeAndState widthSizeAndState = [self resolveSizeAndState:measuredSize.width
+ self.paddingLeft + self.paddingRight
spec:widthMeasureSpec spec:widthMeasureSpec
childMeasuredState:0]; childMeasuredState:0];
DoricSizeAndState heightSizeAndState = [self resolveSizeAndState:measuredSize.height DoricSizeAndState heightSizeAndState = [self resolveSizeAndState:measuredSize.height
+ self.paddingTop + self.paddingBottom
spec:heightMeasureSpec spec:heightMeasureSpec
childMeasuredState:0]; childMeasuredState:0];
self.measuredWidth = widthSizeAndState.size; self.measuredWidth = widthSizeAndState.size;