以多个步骤生成贝塞尔广场

时间:2015-02-17 15:52:15

标签: ios uibezierpath

我可以用这个标准代码生成一个正方形。

func drawSquare() {
    let width:CGFloat = 100
    var square = UIBezierPath()
    square.moveToPoint(CGPoint(x: 0, y: 0))
    square.addLineToPoint(CGPoint(x: width, y: 0))
    square.addLineToPoint(CGPoint(x: width, y: width))
    square.addLineToPoint(CGPoint(x: 0, y: width))
    square.lineWidth = 5
    square.closePath()
    UIColor.blackColor().setStroke()
    square.stroke()
}

但是,如果我想分两步绘制每个addLineToPoint()段,则我的方块不再正确绘制。

func drawSquare2Steps() {
    let width:CGFloat = 200
    let mult:CGFloat = 2
    var square = UIBezierPath()
    square.moveToPoint(CGPoint(x: 0, y: 0))

    square.addLineToPoint(CGPoint(x: 100, y: 0)) // 1
    square.addLineToPoint(CGPoint(x: 200, y: 0)) // 2
    square.addLineToPoint(CGPoint(x: 200, y: 100)) // 3
    square.addLineToPoint(CGPoint(x: 200, y: 200)) // 4
    square.addLineToPoint(CGPoint(x: 100, y: 200)) // 5
    square.addLineToPoint(CGPoint(x: 0, y: 200)) //6

    square.lineWidth = 5
    square.closePath() // 7
    UIColor.orangeColor().setStroke()
    square.stroke()
}

这是我看到的路径:

enter image description here

我试图在此图片中描述此功能:

enter image description here

我的最终目标不是绘制正方形而是更复杂的路径。广场简单地说明了我的复杂路径的问题。

1 个答案:

答案 0 :(得分:1)

除了你的视图大小是(100,100)但是方形大小是(200,200)之外没有错,所以你不会看到整个方块。