cdts/xdts-ios 3/TreeHole/CYHResetCode/CYH/QMUIKit/QMUIComponents/QMUIFloatLayoutView.m

120 lines
5.2 KiB
Mathematica
Raw Permalink Normal View History

2023-07-27 09:20:00 +08:00
/**
* Tencent is pleased to support the open source community by making QMUI_iOS available.
* Copyright (C) 2016-2021 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
//
// QMUIFloatLayoutView.m
// qmui
//
// Created by QMUI Team on 2016/11/10.
//
#import "QMUIFloatLayoutView.h"
#import "QMUICore.h"
#define ValueSwitchAlignLeftOrRight(valueLeft, valueRight) ([self shouldAlignRight] ? valueRight : valueLeft)
const CGSize QMUIFloatLayoutViewAutomaticalMaximumItemSize = {-1, -1};
@implementation QMUIFloatLayoutView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self didInitialize];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self didInitialize];
}
return self;
}
- (void)didInitialize {
self.contentMode = UIViewContentModeLeft;
self.minimumItemSize = CGSizeZero;
self.maximumItemSize = QMUIFloatLayoutViewAutomaticalMaximumItemSize;
}
- (CGSize)sizeThatFits:(CGSize)size {
return [self layoutSubviewsWithSize:size shouldLayout:NO];
}
- (void)layoutSubviews {
[super layoutSubviews];
[self layoutSubviewsWithSize:self.bounds.size shouldLayout:YES];
}
- (CGSize)layoutSubviewsWithSize:(CGSize)size shouldLayout:(BOOL)shouldLayout {
NSArray<UIView *> *visibleItemViews = [self visibleSubviews];
if (visibleItemViews.count == 0) {
return CGSizeMake(UIEdgeInsetsGetHorizontalValue(self.padding), UIEdgeInsetsGetVerticalValue(self.padding));
}
// item item
CGPoint itemViewOrigin = CGPointMake(ValueSwitchAlignLeftOrRight(self.padding.left, size.width - self.padding.right), self.padding.top);
CGFloat currentRowMaxY = itemViewOrigin.y;
CGSize maximumItemSize = CGSizeEqualToSize(self.maximumItemSize, QMUIFloatLayoutViewAutomaticalMaximumItemSize) ? CGSizeMake(size.width - UIEdgeInsetsGetHorizontalValue(self.padding), size.height - UIEdgeInsetsGetVerticalValue(self.padding)) : self.maximumItemSize;
NSInteger line = -1;
for (NSInteger i = 0, l = visibleItemViews.count; i < l; i++) {
UIView *itemView = visibleItemViews[i];
CGRect itemViewFrame;
CGSize itemViewSize = [itemView sizeThatFits:maximumItemSize];
itemViewSize.width = MIN(maximumItemSize.width, MAX(self.minimumItemSize.width, itemViewSize.width));
itemViewSize.height = MIN(maximumItemSize.height, MAX(self.minimumItemSize.height, itemViewSize.height));
BOOL shouldBreakline = i == 0 ? YES : ValueSwitchAlignLeftOrRight(itemViewOrigin.x + self.itemMargins.left + itemViewSize.width + self.padding.right > size.width,
itemViewOrigin.x - self.itemMargins.right - itemViewSize.width - self.padding.left < 0);
if (shouldBreakline) {
line++;
currentRowMaxY += line > 0 ? self.itemMargins.top : 0;
// item itemMargins
itemViewFrame = CGRectMake(ValueSwitchAlignLeftOrRight(self.padding.left, size.width - self.padding.right - itemViewSize.width), currentRowMaxY, itemViewSize.width, itemViewSize.height);
itemViewOrigin.y = CGRectGetMinY(itemViewFrame);
} else {
//
itemViewFrame = CGRectMake(ValueSwitchAlignLeftOrRight(itemViewOrigin.x + self.itemMargins.left, itemViewOrigin.x - self.itemMargins.right - itemViewSize.width), itemViewOrigin.y, itemViewSize.width, itemViewSize.height);
}
itemViewOrigin.x = ValueSwitchAlignLeftOrRight(CGRectGetMaxX(itemViewFrame) + self.itemMargins.right, CGRectGetMinX(itemViewFrame) - self.itemMargins.left);
currentRowMaxY = MAX(currentRowMaxY, CGRectGetMaxY(itemViewFrame) + self.itemMargins.bottom);
if (shouldLayout) {
itemView.frame = itemViewFrame;
}
}
// itemMarins.bottom
currentRowMaxY -= self.itemMargins.bottom;
CGSize resultSize = CGSizeMake(size.width, currentRowMaxY + self.padding.bottom);
return resultSize;
}
- (NSArray<UIView *> *)visibleSubviews {
NSMutableArray<UIView *> *visibleItemViews = [[NSMutableArray alloc] init];
for (NSInteger i = 0, l = self.subviews.count; i < l; i++) {
UIView *itemView = self.subviews[i];
if (!itemView.hidden) {
[visibleItemViews addObject:itemView];
}
}
return visibleItemViews;
}
- (BOOL)shouldAlignRight {
return self.contentMode == UIViewContentModeRight;
}
@end