2016-11-10 17 views
0

TableViewで5行のマイルストーンを描画しようとしています。セルに "O"というラベルを付けました。ここで行の高さは44で、これらのドットを接続しようとしています このように O ------ O ------ O ------ O ----- O垂直になりますが、ここで何が欠けているのか分かりません。次のコードを参照してください。Tableviewで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

私はここに何かを逃していますか?あなたはUILabel .Insteadは、円とのように破線のパターンとラインを作成作成する必要がいけない

+0

の詳細情報を投稿してください。問題は "私は望ましい結果を得ることができません"とは何ですか?期待される出力、実際の出力... – shallowThought

+0

詳細が必要な場合は、tableviewcellでコードをコピーし、xibでテーブルビューを取得し、セルをドラッグし、ラベルを貼り付け、マイルストーンにラベルを貼り付けて実行します。 – iSwift

答えて

2

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

EDIT: 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

関連する問題