iOS按比例實(shí)現(xiàn)方塊圖
本文實(shí)例為大家分享了iOS按比例實(shí)現(xiàn)方塊圖的具體代碼,供大家參考,具體內(nèi)容如下
原理:二分法遞歸實(shí)現(xiàn),就是每次“對(duì)半分”,分到只剩兩個(gè)
上代碼: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 里面的每一個(gè)小的視圖
@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布局此方法可不實(shí)現(xiàn) self.valueLabel.frame = self.bounds; self.valueLabel.adjustsFontSizeToFitWidth = YES; } @end
效果圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS實(shí)現(xiàn)簡(jiǎn)單的頭部縮放功能
這篇文章主要介紹了iOS 簡(jiǎn)單的頭部縮放效果,頭部伴隨模糊效果放大縮小,并在一定位置時(shí)懸停充當(dāng)導(dǎo)航欄,本文給大家提供實(shí)現(xiàn)思路,需要的朋友可以參考下2018-08-08一行iOS代碼實(shí)現(xiàn)圖片無(wú)限輪播器
一行iOS代碼實(shí)現(xiàn)圖片無(wú)限輪播器的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05關(guān)于iOS 11的一些新特性適配實(shí)踐總結(jié)
iOS 11 為整個(gè)生態(tài)系統(tǒng)的 UI 元素帶來(lái)了一種更加大膽、動(dòng)態(tài)的新風(fēng)格。下面這篇文章主要給大家總結(jié)介紹了關(guān)于iOS 11的一些新特性適配實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-11-11iOS數(shù)據(jù)持久化KeyChain數(shù)據(jù)操作詳解
這篇文章主要為大家介紹了iOS數(shù)據(jù)持久化KeyChain,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02簡(jiǎn)述iOS屬性中的內(nèi)存管理參數(shù)
這篇文章主要介紹了簡(jiǎn)述iOS屬性中的內(nèi)存管理參數(shù) 的相關(guān)資料,需要的朋友可以參考下2018-02-02Android NavigationController 右滑手勢(shì)詳解
目前蘋果手機(jī)在人機(jī)交互中盡力做到極致,在ios7中,新增了一個(gè)小小功能,用戶不用點(diǎn)擊右上角的返回按鈕,在屏幕左邊一滑,就會(huì)返回。下面給大家詳解Android NavigationController 右滑手勢(shì),需要的朋友可以參考下2015-08-08iOS中讀寫鎖的簡(jiǎn)單實(shí)現(xiàn)方法實(shí)例
讀寫鎖是計(jì)算機(jī)程序的并發(fā)控制的一種同步機(jī)制,也稱“共享-互斥鎖”、多讀者-單寫者鎖,讀操作可并發(fā)重入,寫操作是互斥的,這篇文章主要給大家介紹了關(guān)于iOS中讀寫鎖的簡(jiǎn)單實(shí)現(xiàn)方法,需要的朋友可以參考下2021-11-11