亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

iOS按比例實現(xiàn)方塊圖

瀏覽:12日期:2022-09-16 15:43:49

本文實例為大家分享了iOS按比例實現(xiàn)方塊圖的具體代碼,供大家參考,具體內(nèi)容如下

原理:二分法遞歸實現(xiàn),就是每次“對半分”,分到只剩兩個

上代碼:SZBlockView

@interface SZBlockView : UIView@property (nonatomic, strong) NSArray *data;//數(shù)據(jù)源@end #import 'SZBlockView.h'#import 'SZItemView.h' @implementation SZBlockView - (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) {self.backgroundColor = UIColor.whiteColor; } return self;} -(void)setData:(NSArray *)data{ _data = data; [self removeAll]; for (NSString* value in data) {[self addSubNode:[value intValue]]; } [self recalcLayout];} -(void)addSubNode:(int)value{ SZItemView* item = [SZItemView new]; item.value = value; [self addSubview:item];} -(void)removeAll{ //移除所有子視圖 [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];} -(bool)isVertical:(double)w Height:(double) h{ return w / h > 1.618;//黃金比例,可以自己根據(jù)需求修改} -(void)recalcLayout{ if (self.subviews.count < 1) return; [self recalcSquarifiedLayout:0 Finish:self.subviews.count - 1 Area:self.bounds];} -(void)recalcSliceLayout:(NSUInteger)nStart Finish:(NSUInteger)nFinish Area:(CGRect)rect IsVertical:(bool) bIsVertical{ NSAssert(nStart < self.subviews.count, @'nStart >= self.subviews.count'); NSAssert(nFinish < self.subviews.count, @'nFinish >= self.subviews.count'); if (nStart == nFinish) {[self.subviews[nStart] setFrame:rect];return; } double dblTotal = [self getChildrenTotal:nStart Finish:nFinish]; double x = rect.origin.x; double y = rect.origin.y; if (bIsVertical) {for (NSUInteger i = nStart; i <= nFinish; i++){ SZItemView* item = self.subviews[i]; double cx = rect.size.width * item.value / dblTotal; CGRect rectSubNode = item.frame; rectSubNode = rect; rectSubNode.origin.x = x; if (i == nFinish) {rectSubNode.size.width = cx; }else{rectSubNode.size.width = cx-1; }item.frame = rectSubNode;x += cx;} } else {for (NSUInteger i = nStart; i <= nFinish; i++){ SZItemView* item = self.subviews[i]; double cy = rect.size.height * item.value / dblTotal; CGRect rectSubNode = item.frame; rectSubNode = rect; rectSubNode.origin.y = y; if (i==nFinish) {rectSubNode.size.height = cy; }else{rectSubNode.size.height = cy-1; }item.frame = rectSubNode; y += cy;} }} -(void)recalcSquarifiedLayout:(NSUInteger)nStart Finish:(NSUInteger)nFinish Area:(CGRect) rect{ NSAssert(nStart < self.subviews.count, @'nStart >= self.subviews.count'); NSAssert(nFinish < self.subviews.count, @'nFinish >= self.subviews.count'); if (nStart + 2 > nFinish) {return [self recalcSliceLayout:nStart Finish:nFinish Area:rect IsVertical:[self isVertical:rect.size.width Height:rect.size.height]]; } double total = [self getChildrenTotal:nStart Finish:nFinish],total_left = 0.; for (NSUInteger i = nStart; i <= nFinish; i++) {SZItemView* item = self.subviews[i];double pre_dt = total_left - total / 2;total_left += item.value;double dt = total_left - total / 2; if (dt > 0){ if (dt + pre_dt > 0) {total_left -= item.value;i--; } if ([self isVertical:rect.size.width Height:rect.size.height]) {CGRect rectLeft = rect;rectLeft.size.width = rect.size.width * total_left / total - 1;[self recalcSquarifiedLayout:nStart Finish:i Area:rectLeft]; CGRect rectRight = rect;rectRight.origin.x = rectLeft.origin.x + rectLeft.size.width + 1;rectRight.size.width = rect.size.width - rectLeft.size.width - 1;[self recalcSquarifiedLayout:i + 1 Finish:nFinish Area:rectRight]; } else {CGRect rectTop = rect;rectTop.size.height = rect.size.height * total_left / total - 1;[self recalcSquarifiedLayout:nStart Finish:i Area:rectTop]; CGRect rectBottom = rect;rectBottom.origin.y = rectTop.origin.y + rectTop.size.height + 1;rectBottom.size.height = rect.size.height - rectTop.size.height - 1;[self recalcSquarifiedLayout:i + 1 Finish:nFinish Area:rectBottom]; } return;} } // NSAssert(false, @'unreachable');} -(double)getChildrenTotal:(NSUInteger)nStart Finish:(NSUInteger) nFinish{ double dblTotal = 0.; for (NSUInteger i = nStart; i <= nFinish; i++) {SZItemView* item = self.subviews[i];dblTotal += item.value; } return dblTotal;} @end

SZItemView 里面的每一個小的視圖

@interface SZItemView : UIView@property (nonatomic, assign) int value;//傳入要顯示的值@end #import 'SZItemView.h'@interface SZItemView ()@property (nonatomic, strong) UILabel *valueLabel;@end @implementation SZItemView - (instancetype)init{ self = [super init]; if (self) {[self setupUI]; } return self;}-(void)setupUI{ UILabel *valueLabel = [[UILabel alloc] initWithFrame:self.frame]; valueLabel.adjustsFontSizeToFitWidth = YES; self.valueLabel = valueLabel; valueLabel.textAlignment = NSTextAlignmentCenter; valueLabel.textColor = UIColor.whiteColor; [self addSubview:valueLabel];} - (void)setValue:(int)value{ _value = value; self.valueLabel.text = [NSString stringWithFormat:@'%d',value]; self.backgroundColor = UIColor.orangeColor;}- (void)layoutSubviews{//如果用masonry布局此方法可不實現(xiàn) self.valueLabel.frame = self.bounds; self.valueLabel.adjustsFontSizeToFitWidth = YES;}@end

效果圖:

iOS按比例實現(xiàn)方塊圖

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: IOS
相關文章:
主站蜘蛛池模板: 国产区一区 | 成年人黄色一级片 | 永久在线播放 | 青娱乐久草 | 三级毛片在线免费观看 | 亚洲国产成人久久综合一 | 中文字幕第一页面 | 99久久精品国产国产毛片 | 欧美日韩第三页 | 亚洲国产成人精品不卡青青草原 | 亚洲欧美日韩国产一区图片 | 免费在线看黄视频 | 国产精品嫩草影院午夜 | 免费 欧美 自拍 在线观看 | 免费播放国产性色生活片 | 一级黄色片免费看 | 成人免费高清视频网址 | 北条麻妃一区二区三区 | 日韩黄色在线观看 | 日韩国产毛片 | 女人毛片在线 | 国产免费看网站v片不遮挡 国产免费高清 | 一级特级欧美aa毛片免费 | jiucao在线观看精品 | 免费观看性欧美毛片 | 亚洲欧美国产日产综合不卡 | 国产人成精品综合欧美成人 | 亚洲精品国产专区91在线 | 日韩欧美a级高清毛片 | 午夜一级毛片免费视频 | 真人毛片免费拍拍拍aa视频 | 亚洲毛片大全 | 狠狠色丁香婷婷久久综合不卡 | 久草综合视频 | 国产色婷婷精品综合在线观看 | 丝袜美足国产一区在线 | 综合久久婷婷 | 美女污污网站 | 91短视频免费版 | 亚洲精品不卡午夜精品 | 一级片一级片一级片一级片 |