2016-11-10 68 views
1

我正在嘗試將一列UIBezierPath作爲點格顯示。點的間距是相對於UIView的大小,是這樣的:(bounds.UIView/10),使間距10每側如何描述相對於UIView大小的網格點?

所以基本上我有兩個問題:

1)如何行程單點使用UIBezierPath

2)如何創建一個點陣列並在UIView上敲擊它們。

回答

0

這裏是示例夫特3

func addDashedLine(layer: CALayer, dotSize: CGFloat) { 
    let p0 = CGPoint(x: 0, y: layer.bounds.height/2) 
    let p1 = CGPoint(x: layer.bounds.width, y: layer.bounds.height/2) 

    let path = UIBezierPath() 
    path.move(to:p0) 
    path.addLine(to:p1) 
    path.stroke() 

    let stepLength = Float(layer.bounds.width/10.0) 
    let dashPattern = [NSNumber(value: 0.001), NSNumber(value: stepLength)] 

    let shapeLayer = CAShapeLayer() 
    shapeLayer.frame = layer.bounds 
    shapeLayer.strokeColor = UIColor.red.cgColor 
    shapeLayer.lineWidth = dotSize 
    shapeLayer.lineJoin = kCALineJoinRound 
    shapeLayer.lineCap = kCALineCapRound 
    shapeLayer.lineDashPattern = dashPattern 
    shapeLayer.lineDashPhase = dotSize 
    shapeLayer.path = path.cgPath 

    layer.addSublayer(shapeLayer) 
} 

此示例使用一個簡單的路徑繪製水平線。如果您需要用固定數量的點畫一條線,則需要首先計算它的長度,然後將其除以要繪製的點的數量。

用法:

override func viewDidLoad() { 
    super.viewDidLoad() 
    addDashedLine(layer: view.layer, dotSize: 3) 
} 

輸出:

enter image description here