2016-11-10 111 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