画线对称间隔开(swift4)

时间:2018-01-10 19:09:00

标签: ios line swift4 uibezierpath

我试图像松散的叶纸一样画出空间。我试图找到一个关于如何划分线条的序列。我下面的代码是我正在寻找的帽子的一个例子。问题是如果我按照将1/8添加到下一行的顺序,并且线条继续靠近在一起。 enter image description here

    import UIKit

class draw: UIView {

var line = UIBezierPath()
var line3 = UIBezierPath()
var lin2 = UIBezierPath()
var line1 = UIBezierPath()
func grapher() {
   line.move(to: .init(x:0, y: bounds.height/1.5))
    line.addLine(to: .init(x: bounds.width, y: bounds.height / 1.5))
    UIColor.brown.setStroke()
    line.lineWidth = 2
    line.stroke()


    line3.move(to: .init(x:0, y: bounds.height / 1.375))
    line3.addLine(to: .init(x: bounds.width, y: bounds.height / 1.375))
    UIColor.red.setStroke()
    line3.lineWidth = 2
    line3.stroke()

    line3.move(to: .init(x:0, y: bounds.height / 1.25 ))
    line3.addLine(to: .init(x: bounds.width, y: bounds.height / 1.25))
    UIColor.red.setStroke()
    line3.lineWidth = 2
    line3.stroke()

    line3.move(to: .init(x:0, y: bounds.height / 1.125 ))
    line3.addLine(to: .init(x: bounds.width, y: bounds.height / 1.125))
    UIColor.red.setStroke()
    line3.lineWidth = 2
    line3.stroke()

 }
override func draw(_ rect: CGRect) {
    grapher()
}
}

1 个答案:

答案 0 :(得分:0)

我想我会使用加法而不是除法:

let step = bounds.height / 8 // or however big you want the gap to be
var yForLine = step

// draw your first line line using yForLine

yForLine += step

// draw next line using yForLine

// etc.