自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

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

 


Qinz

1.首先我們創(chuàng)建一個(gè)UIView,在上面放一個(gè)tableView,聲明一個(gè)方法,傳遞兩個(gè)參數(shù),***個(gè)參數(shù)是需要將該時(shí)間軸放在哪個(gè)視圖上,第二個(gè)參數(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的應(yīng)用,這里有一個(gè) [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[TimeLineCell class]contentViewWidth:self.frame.size.width]方法是用到了SDAutoLayout這個(gè)庫用來自動計(jì)算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 -- 每組返回多少個(gè) 
  26. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
  27.     return self.dataArr.count
  28. #pragma mark -- 每個(gè)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 -- 每個(gè)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 -- 選擇每個(gè)cell執(zhí)行的操作 
  48. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
  49.     [tableView deselectRowAtIndexPath:indexPath animated:NO]; 

3.關(guān)鍵在于tableViewCell布局,采用了Xib,方便對樣式進(jìn)行設(shè)置,布局依然采用的是SDAutoLayout這個(gè)庫

 


圖片.png

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

  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]; 

總結(jié):整體主要采用tableView進(jìn)行布局,然后讓cell的高度自適應(yīng),需要注意的地方就是Cell中時(shí)間軸中的線條需要保持連貫,所以需要對***個(gè)cell進(jìn)行判斷,讓線條剛好與原點(diǎn)連接。

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

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

責(zé)任編輯:未麗燕 來源: 簡書
相關(guān)推薦

2020-10-30 15:04:16

開發(fā)技能代碼

2021-06-07 12:08:06

iOS Python API

2020-09-14 11:30:26

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

2020-11-23 16:23:59

CSS設(shè)計(jì)技術(shù)

2012-06-28 10:26:51

Silverlight

2025-03-13 06:22:59

2020-06-30 10:45:28

Web開發(fā)工具

2021-04-30 16:23:58

WebRTC實(shí)時(shí)音頻

2022-06-17 08:05:28

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

2018-11-08 13:53:15

Flink程序環(huán)境

2010-12-10 17:23:56

IBMIaaS

2021-06-02 09:12:04

App自動化測試測試自動化

2021-01-06 05:23:15

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

2014-04-15 11:19:19

2016-09-12 17:28:45

云存儲應(yīng)用軟件存儲設(shè)備

2019-12-23 16:42:44

JavaScript前端開發(fā)

2011-07-11 09:58:52

2021-10-19 07:27:08

HTTP代理網(wǎng)絡(luò)

2021-06-18 07:34:12

Kafka中間件微服務(wù)

2020-10-13 18:22:58

DevOps工具開發(fā)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號