iOS开发之带你5分钟封装一个时间轴

百家 作者:iOS开发 2018-05-13 11:16:51

点击上方“iOS开发”,选择“置顶公众号”

关键时刻,第一时间送达!


时间轴在一些app中用的场景还不少,原理实现起来较为简单,下面我们就来动手封装一个比较常用的时间轴,具体效果看下图:

Qinz


  • 1.首先我们创建一个UIView,在上面放一个tableView,声明一个方法,传递两个参数,第一个参数是需要将该时间轴放在哪个视图上,第二个参数是传递数据源,头文件下:



#import
@interface QinzTimeLine : UIView
@property (nonatomic, strong) NSArray *titleArr;
-(void)setSuperView:(UIView*)superView DataArr:(NSMutableArray*)dataArr;
@end


  • 2.我们再来看看.m文件,也就是最简单的tableView的应用,这里有一个 [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[TimeLineCell class] contentViewWidth:self.frame.size.width]方法是用到了SDAutoLayout这个库用来自动计算cell高度的


#import "QinzTimeLine.h"
#import "SDAutoLayout.h"
#import "TimeLineCell.h"
@interface QinzTimeLine ()< UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@end
@implementation QinzTimeLine
-(void)setSuperView:(UIView *)superView DataArr:(NSMutableArray *)dataArr{
   self.frame = superView.bounds;
   [superView addSubview:self];
   [self setUp];
   self.dataArr = dataArr;
}
-(void)setUp{
   self.tableView = [[UITableView alloc]init];
   self.tableView.delegate = self;
   self.tableView.dataSource = self;
   self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
   [self addSubview:self.tableView];
   self.tableView.sd_layout.topEqualToView(self).leftEqualToView(self).bottomEqualToView(self).rightEqualToView(self);
}
#pragma mark -- tableView的代理方法
#pragma mark -- 返回多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return 1;
}
#pragma mark -- 每组返回多少个
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return self.dataArr.count;
}
#pragma mark -- 每个cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   TimeLineModel*model = self.dataArr[indexPath.row];
   return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[TimeLineCell class] contentViewWidth:self.frame.size.width];
}
#pragma mark -- 每个cell显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   TimeLineCell*cell = [TimeLineCell timeLineCell:tableView];
   if (indexPath.row == 0) {
       cell.lineView.sd_layout.topSpaceToView(cell.pointView, 0);
       cell.lineView.backgroundColor = [UIColor grayColor];
       cell.pointView.backgroundColor = [UIColor redColor];
   }else{
       cell.lineView.sd_layout.topSpaceToView(cell.contentView, 0);
       cell.pointView.backgroundColor = [UIColor grayColor];
       cell.lineView.backgroundColor = [UIColor grayColor];
   }
   cell.model = self.dataArr[indexPath.row];
   cell.selectionStyle = UITableViewCellSelectionStyleNone;
   return cell;
}
#pragma mark -- 选择每个cell执行的操作
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   [tableView deselectRowAtIndexPath:indexPath animated:NO];
}


  • 3.关键在于tableViewCell布局,采用了Xib,方便对样式进行设置,布局依然采用的是SDAutoLayout这个库


图片.png


  • 4.看下布局代码,这里对titleLB的布局做高度自适应,及设置autoHeightRatio为0即可,然后我们直接在设置模型中调用  [self setupAutoHeightWithBottomView:self.titleLB bottomMargin:0]就自动完成了高度自适应,是不是很方便


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


  • 5.到此,封装完毕,最后我们来看看控制器调用代码


- (void)viewDidLoad {
   [super viewDidLoad];
   self.automaticallyAdjustsScrollViewInsets = YES;
   self.timeLine = [[QinzTimeLine alloc]init];
   [self setData];
   [self.timeLine setSuperView:self.view DataArr:self.dataArr];
}


总结:整体主要采用tableView进行布局,然后让cell的高度自适应,需要注意的地方就是Cell中时间轴中的线条需要保持连贯,所以需要对第一个cell进行判断,让线条刚好与原点连接。


最后,附上demo供参考:
https://gitee.com/Qinz_323/qinztimeline

我是Qinz,希望我的文章对你有帮助。



  • 作者:Qinz

  • 链接:https://www.jianshu.com/p/bb6e9bf2bcc1

  • iOS开发整理发布,转载请联系作者授权

【点击成为源码大神】

关注公众号:拾黑(shiheibook)了解更多

[广告]赞助链接:

四季很好,只要有你,文娱排行榜:https://www.yaopaiming.com/
让资讯触达的更精准有趣:https://www.0xu.cn/

公众号 关注网络尖刀微信公众号
随时掌握互联网精彩
赞助链接