2016-11-10 80 views
0

我想要在TableView中绘制一个具有5行的里程碑,我在单元格中添加了一个标签“O”,此处行高为44,并试图连接这些圆点 Something像这样 O ------ O ------ O ------ O ----- O垂直,但我无法弄清楚这里缺少什么,请参考以下代码在桌面视图中使用BezierPath创建时间轴视图

class StopslistTableViewCell:UITableViewCell{ 

@IBOutlet weak var milestoneLbl: UILabel! 

override func draw(_ rect: CGRect) { 

    let scrrenX = UIScreen.main.bounds.width - 30 

    let frametosuperview = milestoneLbl.convert(milestoneLbl!.frame, to: self.superview) 

    print("dotframetosuperview",frametosuperview) 

    //print(CGPoint(x: scrrenX, y: previousposY + rect.size.height)) 

    //frametosuperview.origin.y > 40 ? frametosuperview.origin.y - 20 : frametosuperview.origin.y 

    let path = UIBezierPath() 
    path.move(to: CGPoint(x: scrrenX, y:frametosuperview.origin.y < 30 ? frametosuperview.origin.y : frametosuperview.origin.y - frametosuperview.height)) 

    print("move to point--->",frametosuperview.origin.y < 30 ? frametosuperview.origin.y : frametosuperview.origin.y - frametosuperview.height) 

    path.addLine(to: CGPoint(x: scrrenX, y: frametosuperview.origin.y < 30 ? frametosuperview.height + 24 : (frametosuperview.origin.y + 24))) 
    print("add line to point--->",CGPoint(x: scrrenX, y: frametosuperview.origin.y < 30 ? frametosuperview.height + 24 : (frametosuperview.origin.y + 24))) 

    path.lineWidth = 5 

    UIColor.red.setStroke() 
    path.stroke() 

    setNeedsDisplay() 
} 

调试输出

dotframetosuperview (560.0, 20.0, 20.0, 20.0) 
move to point---> 20.0 
add line to point---> (290.0, 44.0) 
dotframetosuperview (560.0, 64.0, 20.0, 20.0) 
move to point---> 44.0 
add line to point---> (290.0, 88.0) 
dotframetosuperview (560.0, 108.0, 20.0, 20.0) 
move to point---> 88.0 
add line to point---> (290.0, 132.0) 
dotframetosuperview (560.0, 152.0, 20.0, 20.0) 
move to point---> 132.0 
add line to point---> (290.0, 176.0) 
dotframetosuperview (560.0, 196.0, 20.0, 20.0) 
move to point---> 176.0 
add line to point---> (290.0, 220.0) 

我需要像这样

enter image description here

我无法得到想要的结果,我在这里错过了什么..?

+0

请张贴更多的信息。有什么问题“我无法获得理想的结果”?预期的输出,实际的输出... – shallowThought

+0

如果你想要更多的信息,复制tableviewcell中的代码,在xib中获取一个tableview,拖拽一个单元格,在其上放置一个标签,将标签附加到里程碑并运行。 – iSwift

回答

2

你不需要创建UILabel。相反创建一个圆圈,并用虚线图案为线:

let offSet:CGFloat = 20.0 
let circleRadius:CGFloat = 5.0 

override func draw(_ rect: CGRect) { 

    //creating a circle 
    let circlePath = UIBezierPath(arcCenter: CGPoint(x: offSet,y: circleRadius), radius: CGFloat(circleRadius), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true) 
    let shapeLayer = CAShapeLayer() 
    shapeLayer.path = circlePath.cgPath 
    shapeLayer.fillColor = UIColor.clear.cgColor 
    shapeLayer.strokeColor = UIColor.black.cgColor 
    shapeLayer.lineWidth = 2.0 
    circlePath.stroke() 

    //creating a line with dashed pattern 
    let dashPath = UIBezierPath() 
    let startPoint = CGPoint(x:offSet ,y:circleRadius * 2) 
    dashPath.move(to: startPoint) 

    let endPoint = CGPoint(x:offSet,y: 
     self.bounds.maxY) 
    dashPath.addLine(to: endPoint) 

    let dashes: [ CGFloat ] = [4, 1] //line with dash pattern of 4 thick and i unit space 
    dashPath.setLineDash(dashes, count: dashes.count, phase: 0) 
    dashPath.lineWidth = 2.0 
    dashPath.lineCapStyle = .butt 
    UIColor.black.set() 
    dashPath.stroke() 

} 

输出: enter image description here

编辑:由于OP希望时间表圈从细胞的中心出现。

var allRows = Int() 
var currentIndexPath = IndexPath() 
let offSet:CGFloat = 20.0 
let circleRadius:CGFloat = 5.0 


override func draw(_ rect: CGRect) { 

    //creating a circle 
    let circlePath = UIBezierPath(arcCenter: CGPoint(x: offSet,y: self.bounds.midY), radius: CGFloat(circleRadius), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true) 
    let shapeLayer = CAShapeLayer() 
    shapeLayer.path = circlePath.cgPath 
    shapeLayer.fillColor = UIColor.clear.cgColor 
    shapeLayer.strokeColor = UIColor.black.cgColor 
    shapeLayer.lineWidth = 2.0 
    circlePath.stroke() 

    //creating a line with dashed pattern 
    let dashPath = UIBezierPath() 
    var startPoint = CGPoint() 

    if currentIndexPath.row == 0{ 

     startPoint = CGPoint(x:offSet ,y:self.bounds.midY) 
    }else{ 

     startPoint = CGPoint(x:offSet ,y:0) 
    } 
    dashPath.move(to: startPoint) 

    var endPoint = CGPoint() 

    if currentIndexPath.row == allRows-1{ 

     endPoint = CGPoint(x:offSet,y: 
      self.bounds.midY) 
    }else{ 

     endPoint = CGPoint(x:offSet,y: 
      self.bounds.maxY) 

    } 
    dashPath.addLine(to: endPoint) 

    let dashes: [ CGFloat ] = [4, 1] //line with dash pattern of 4 thick and i unit space 
    dashPath.setLineDash(dashes, count: dashes.count, phase: 0) 
    dashPath.lineWidth = 2.0 
    dashPath.lineCapStyle = .butt 
    UIColor.black.set() 
    dashPath.stroke() 
} 

和你cellForRowAt indexPath应该被配置为

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     ....... 
     cell.allRows = totalNumberOfRows 
     cell.currentIndexPath = indexPath 
     return cell 


    } 

这将导致以下O/P: enter image description here

+0

以及它确实回答了这个问题,但我对这个问题的意图是,“为什么这个代码没有按照我的意图绘制,因为x,y线似乎是正确的”,我也使用了标签,所以我没有每当我移动里程碑的位置时,重置圆圈框架 – iSwift

+0

@iSwift您正在构建的解决方案仅仅是一个破解UILabel和虚线的破解。所以,我想我会在一个更好的答案中发布一个更好的答案正确的方式。 –

+0

让我说要把圆圈放在中间,但是线条的起点应该是从第一个圆心到末圆圆心 – iSwift