這是一個關於OC時間軸的簡單實現,我認為重要的是思路。
感謝作者:Cyandev 的文章《iOS 實現時間線列表效果》給的思路。這里先附上Objective-C的代碼實現,有時間再去試試Swift
先看一下效果:

再看一段主要的代碼:
//根據cell判斷cell中bottomLine的顏色,如果不是最后一個,則顏色和topLine顏色一樣。
cell.buttomLine.backgroundColor = indexPath.row == (_dataArray.count-1) ? [UIColor grayColor] : cell.topLine.backgroundColor; self.topLine.backgroundColor = cell.topLine.backgroundColor; // cell.topLine.backgroundColor = indexPath.row == 0 ? [UIColor clearColor] : self.topLine.backgroundColor; //使創建的 topLine 視圖背景顏色 等於 cell中 topLine 的背景顏色
self.topLine.backgroundColor = cell.topLine.backgroundColor; //獲取cell中topLine 或者bottomLine 的 x 位置。
/** * 將像素point由point所在視圖轉換到目標視圖view中,返回在目標視圖view中的像素值 * (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view */ self.leadingSpaceOfLines = [cell convertPoint:cell.topLine.frame.origin toView:self.view].x; [self scrollViewDidScroll:tableView];
#pragma mark --important code--
//根據上下拉動,動態改變 topLine 和 bottomLine 的 y 軸坐標。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { _topLine.frame = CGRectMake(_leadingSpaceOfLines, 0, 3, -scrollView.contentOffset.y); CGFloat yOffSet = scrollView.frame.size.height - scrollView.contentSize.height + scrollView.contentOffset.y ; _bottomLine.frame = CGRectMake(_leadingSpaceOfLines, self.view.frame.size.height - yOffSet, 3, self.view.frame.size.height); }
這里是我的GitHub上的demo:TimeLine
