国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

iOS如何實(shí)現九宮格自動(dòng)生成視圖

發(fā)布時(shí)間:2021-09-27 17:50 來(lái)源:億速云 閱讀:0 作者:小新 欄目: 開(kāi)發(fā)技術(shù)

這篇文章主要為大家展示了“iOS如何實(shí)現九宮格自動(dòng)生成視圖”,內容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習一下“iOS如何實(shí)現九宮格自動(dòng)生成視圖”這篇文章吧。

首先肯定是用一個(gè)類(lèi)來(lái)管理整個(gè)模塊的,所以創(chuàng )建一個(gè)UISodokuView類(lèi)繼承于UIScrollView:——為什么是scollView?——因為當需要添加的控件數量較大時(shí),顯然會(huì )超出手機屏幕范圍,只有用scrollView才能完全顯示,也就是說(shuō),只要用戶(hù)提供了單個(gè)控件的frame、控件數量以及每一行控件的個(gè)數,就能夠確定UIScrollView的contentSize大小,從而添加。

UISodokuView類(lèi)

.h文件

@interface UISodokuView : UIScrollView//基礎控件的frame@property(nonatomic,assign)CGRect itemFrame;//要添加的控件數量@property(nonatomic,assign)NSInteger itemsNumber;//每一行控件數量@property(nonatomic,assign)NSInteger itemsNumberInOneLine;//存儲控件的array@property(nonatomic,strong)NSMutableArray *itemsArray;//scrollView寬度@property(nonatomic,assign)NSInteger scrollViewWidth;//scrollView高度@property(nonatomic,assign)NSInteger scrollViewHeight;//初始化,但并沒(méi)有添加控件-(instancetype)initWithItemFrame:(CGRect)frame andItemsNumber:(NSInteger)itemsNumber andItemsNumberInOneLine:(NSInteger)itemsInOneLine;

這里我添加到scrollView上面每一個(gè)控件是一個(gè)默認背景為白色的UIView對象,并存儲到itemsArray里面,用戶(hù)想讓每一個(gè)控件顯示什么可以通過(guò)獲取數組對象進(jìn)行再添加。

.m文件

@implementation UISodokuView-(instancetype)initWithItemFrame:(CGRect)frame andItemsNumber:(NSInteger)itemsNumber andItemsNumberInOneLine:(NSInteger)itemsInOneLine{ self = [super init]; if (self) { //初始化 _itemsArray = [NSMutableArray array]; _itemFrame = frame; _itemsNumber = itemsNumber; _itemsNumberInOneLine = itemsInOneLine; self.frame = CGRectZero; } [self layoutModule]; return self;}-(void)layoutModule{ //獲取item寬高和橫向縱向間距 NSInteger itemWidthGap = _itemFrame.origin.x; NSInteger itemHeightGap = _itemFrame.origin.y; NSInteger width = _itemFrame.size.width; NSInteger height = _itemFrame.size.height; //容器寬度 _scrollViewWidth = itemWidthGap * (_itemsNumberInOneLine + 1) + width * _itemsNumberInOneLine; //總行數 NSInteger numberOfLines = 0; if (_itemsNumber%_itemsNumberInOneLine == 0) { numberOfLines = _itemsNumber/_itemsNumberInOneLine; }else{ numberOfLines = _itemsNumber/_itemsNumberInOneLine + 1; } _scrollViewHeight = itemHeightGap*(numberOfLines + 1) + height*numberOfLines - 2; //確定scrollView的frame,默認y軸邊距200 self.frame = CGRectMake(0, 200, _scrollViewWidth,height + itemHeightGap*2); self.contentSize = CGSizeMake(_scrollViewWidth, _scrollViewHeight); self.scrollEnabled = YES; self.backgroundColor = [UIColor lightGrayColor]; //創(chuàng  )建并添加控件 NSInteger line = 1; NSInteger row = 1; for (int i = 1;i <= _itemsNumber ; i++) { UIView *item = [[UIView alloc] initWithFrame:_itemFrame]; item.backgroundColor = [UIColor whiteColor]; [_itemsArray addObject:item]; [self addSubview:item]; //判斷處于第幾行 line = i/_itemsNumberInOneLine + 1; //判斷處于第幾列 row = i % _itemsNumberInOneLine; if (row == 0) {  row = _itemsNumberInOneLine;  line -= 1; } item.frame = CGRectMake(row*itemWidthGap + (row-1)*width, line*itemHeightGap + (line-1)*height, width, height); }}

這里有些數據是默認的:

——scrollView的可視范圍:寬度由控件frame確定,高度默認顯示一行控件,可滾動(dòng),——scrollView位置默認左邊距為0,上邊距為200;

這些都可由用戶(hù)根據自己情況作更改,所以相當方便。

一下是一個(gè)使用例子:

UISodokuView * sv = [[UISodokuView alloc] initWithItemFrame:CGRectMake(10, 10, 100, 120) andItemsNumber:10 andItemsNumberInOneLine:3]; [self.view addSubview:sv];

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。

iOS
中文天堂最新版www| 做床爱无遮挡免费视频| 精品无码中文视频在线观看| 国产激情久久久久影院老熟女免费| 粉嫩大学生无套内射无码卡视频| 狼狼综合久久久久综合网|