成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

iOS開發(fā)之帶你5分鐘封裝一個時間軸

移動開發(fā)
時間軸在一些app中用的場景還不少,原理實現(xiàn)起來較為簡單,下面我們就來動手封裝一個比較常用的時間軸

時間軸在一些app中用的場景還不少,原理實現(xiàn)起來較為簡單,下面我們就來動手封裝一個比較常用的時間軸,具體效果看下圖:

 


Qinz

1.首先我們創(chuàng)建一個UIView,在上面放一個tableView,聲明一個方法,傳遞兩個參數(shù),***個參數(shù)是需要將該時間軸放在哪個視圖上,第二個參數(shù)是傳遞數(shù)據(jù)源,頭文件下:

  1. #import <UIKit/UIKit.h> 
  2. @interface QinzTimeLine : UIView 
  3. @property (nonatomic, strong) NSArray *titleArr; 
  4. -(void)setSuperView:(UIView*)superView DataArr:(NSMutableArray*)dataArr; 
  5. @end 

2.我們再來看看.m文件,也就是最簡單的tableView的應用,這里有一個 [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[TimeLineCell class]contentViewWidth:self.frame.size.width]方法是用到了SDAutoLayout這個庫用來自動計算cell高度的

  1. #import "QinzTimeLine.h" 
  2. #import "SDAutoLayout.h" 
  3. #import "TimeLineCell.h" 
  4. @interface QinzTimeLine ()<UITableViewDelegate,UITableViewDataSource> 
  5. @property (nonatomic, strong) UITableView *tableView; 
  6. @property (nonatomic, strong) NSMutableArray *dataArr; 
  7. @end 
  8. @implementation QinzTimeLine 
  9. -(void)setSuperView:(UIView *)superView DataArr:(NSMutableArray *)dataArr{ 
  10.     self.frame = superView.bounds; 
  11.     [superView addSubview:self]; 
  12.     [self setUp]; 
  13.     self.dataArr = dataArr; 
  14. -(void)setUp{ 
  15.     self.tableView = [[UITableView alloc]init]; 
  16.     self.tableView.delegate = self; 
  17.     self.tableView.dataSource = self; 
  18.     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 
  19.     [self addSubview:self.tableView]; 
  20.     self.tableView.sd_layout.topEqualToView(self).leftEqualToView(self).bottomEqualToView(self).rightEqualToView(self); 
  21. #pragma mark -- tableView的代理方法 
  22. #pragma mark -- 返回多少組 
  23. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
  24.     return 1; 
  25. #pragma mark -- 每組返回多少個 
  26. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
  27.     return self.dataArr.count
  28. #pragma mark -- 每個cell的高度 
  29. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
  30.     TimeLineModel*model = self.dataArr[indexPath.row]; 
  31.     return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[TimeLineCell class] contentViewWidth:self.frame.size.width]; 
  32. #pragma mark -- 每個cell顯示的內(nèi)容 
  33. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
  34.     TimeLineCell*cell = [TimeLineCell timeLineCell:tableView]; 
  35.     if (indexPath.row == 0) { 
  36.         cell.lineView.sd_layout.topSpaceToView(cell.pointView, 0); 
  37.         cell.lineView.backgroundColor = [UIColor grayColor]; 
  38.         cell.pointView.backgroundColor = [UIColor redColor]; 
  39.     }else
  40.         cell.lineView.sd_layout.topSpaceToView(cell.contentView, 0); 
  41.         cell.pointView.backgroundColor = [UIColor grayColor]; 
  42.         cell.lineView.backgroundColor = [UIColor grayColor]; 
  43.     } 
  44.     cell.model = self.dataArr[indexPath.row]; 
  45.     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
  46.     return cell; 
  47. #pragma mark -- 選擇每個cell執(zhí)行的操作 
  48. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
  49.     [tableView deselectRowAtIndexPath:indexPath animated:NO]; 

3.關鍵在于tableViewCell布局,采用了Xib,方便對樣式進行設置,布局依然采用的是SDAutoLayout這個庫

 


圖片.png

4.看下布局代碼,這里對titleLB的布局做高度自適應,及設置autoHeightRatio為0即可,然后我們直接在設置模型中調(diào)用 [self setupAutoHeightWithBottomView:self.titleLB bottomMargin:0]就自動完成了高度自適應,是不是很方便

  1. - (void)awakeFromNib { 
  2.     [super awakeFromNib]; 
  3.     self.pointView.sd_layout.topSpaceToView(self.contentView, 20).leftSpaceToView(self.contentView, 5).widthIs(8).heightEqualToWidth(); 
  4.     self.pointView.sd_cornerRadius = @(4); 
  5.     self.lineView.sd_layout.topEqualToView(self.contentView).centerXEqualToView(self.pointView).widthIs(1).bottomSpaceToView(self.contentView, 0); 
  6.     self.ttimeLB.sd_layout.centerYEqualToView(self.pointView).leftSpaceToView(self.pointView, 10).rightSpaceToView(self.contentView, 10).heightIs(20); 
  7.     self.titleLB.sd_layout.topSpaceToView(self.ttimeLB, 15).leftEqualToView(self.ttimeLB).rightSpaceToView(self.contentView, 10).autoHeightRatio(0); 
  8. -(void)setModel:(TimeLineModel *)model{ 
  9.     _model = model; 
  10.     self.titleLB.text=  model.title; 
  11.     self.ttimeLB.text = model.time
  12.     [self setupAutoHeightWithBottomView:self.titleLB bottomMargin:0]; 

5.到此,封裝完畢,***我們來看看控制器調(diào)用代碼

  1. - (void)viewDidLoad { 
  2.     [super viewDidLoad]; 
  3.     self.automaticallyAdjustsScrollViewInsets = YES; 
  4.     self.timeLine = [[QinzTimeLine alloc]init]; 
  5.     [self setData]; 
  6.     [self.timeLine setSuperView:self.view DataArr:self.dataArr]; 

總結:整體主要采用tableView進行布局,然后讓cell的高度自適應,需要注意的地方就是Cell中時間軸中的線條需要保持連貫,所以需要對***個cell進行判斷,讓線條剛好與原點連接。

***,附上demo供參考:https://gitee.com/Qinz_323/qinztimeline

我是Qinz,希望我的文章對你有幫助。

責任編輯:未麗燕 來源: 簡書
相關推薦

2020-10-30 15:04:16

開發(fā)技能代碼

2021-06-07 12:08:06

iOS Python API

2020-09-14 11:30:26

HTTP3運維互聯(lián)網(wǎng)

2020-11-23 16:23:59

CSS設計技術

2012-06-28 10:26:51

Silverlight

2025-03-13 06:22:59

2020-06-30 10:45:28

Web開發(fā)工具

2022-06-17 08:05:28

Grafana監(jiān)控儀表盤系統(tǒng)

2021-04-30 16:23:58

WebRTC實時音頻

2021-06-02 09:12:04

App自動化測試測試自動化

2021-01-06 05:23:15

ServiceMesh網(wǎng)絡阿帕網(wǎng)

2010-12-10 17:23:56

IBMIaaS

2018-11-08 13:53:15

Flink程序環(huán)境

2016-09-12 17:28:45

云存儲應用軟件存儲設備

2019-12-23 16:42:44

JavaScript前端開發(fā)

2021-10-19 07:27:08

HTTP代理網(wǎng)絡

2011-07-11 09:58:52

2014-04-15 11:19:19

2020-10-13 18:22:58

DevOps工具開發(fā)

2021-06-18 07:34:12

Kafka中間件微服務
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 99在线精品视频 | 国产亚洲精品久久yy50 | 国产成人精品一区二区三区 | 久久成人免费观看 | 黄网站免费在线 | 岛国在线免费观看 | 精品一区二区免费视频 | 国产高清精品一区二区三区 | 最新日韩av| 亚洲精品一区二区三区中文字幕 | 欧美精品成人一区二区三区四区 | 黄片毛片免费观看 | 一区二区三区视频播放 | 国产精品永久 | 精品不卡 | 国产精品美女www爽爽爽 | 色99视频 | 久久久九九九九 | 中文字幕国产 | 久久网亚洲 | 欧美日韩在线一区二区三区 | 欧美aaaaaaaa| 日韩在线免费电影 | 久久精品亚洲精品国产欧美 | 乱一性一乱一交一视频a∨ 色爱av | 久久久久久色 | 欧美日韩国产一区 | 国产精品久久久久一区二区三区 | 国产欧美精品一区 | 一区二区福利视频 | 91看片免费版 | 亚洲成年影院 | 中文字幕av色 | 亚洲精彩视频在线观看 | 国产精品久久网 | 九九爱这里只有精品 | 黄色网址免费在线观看 | 日韩毛片在线视频 | ririsao久久精品一区 | 97精品一区二区 | 中文字幕在线观看www |