CGPath 在 Swift 3 中不工作

CGPath not working in Swift 3

我正在更新 swift 的代码 3. 我已经为我的 spritekit 对象创建了一个自定义路径,该路径在 swift 2. 但是,现在我得到一个编译器错误:

Nil is not compatible with expected argument type 'Unsafe Pointer CGAffineTransform'

let offsetX = player.size.width * player.anchorPoint.x
let offsetY = player.size.height * player.anchorPoint.y

let path = CGMutablePath()

CGPathMoveToPoint(path, nil, 10 - offsetX, 16 - offsetY)
CGPathAddLineToPoint(path, nil, 10 - offsetX, 0 - offsetY)
CGPathAddLineToPoint(path, nil, 24 - offsetX, 0 - offsetY)
CGPathAddLineToPoint(path, nil, 24 - offsetX, 16 - offsetY)
CGPathAddLineToPoint(path, nil, 24 - offsetX, 16 - offsetY)
CGPathAddLineToPoint(path, nil, 28 - offsetX, 40 - offsetY)
CGPathAddLineToPoint(path, nil, 18 - offsetX, 46 - offsetY)
CGPathAddLineToPoint(path, nil, 6 - offsetX, 36 - offsetY)
CGPathAddLineToPoint(path, nil, 6 - offsetX, 18 - offsetY)



path.closeSubpath()

错误是在添加到路径时在第二个参数中传递了 nil。我试图这样传递一个不安全的指针:

var tr = CGAffineTransform.identity
CGPathMoveToPoint(path, &tr, 10 - offsetX, 16 - offsetY)
....

但是又遇到了一个奇怪的错误。

CGPathMoveToPoint is unavailable. Use move(to: transform:)

但是,没有参数名为 to 的移动函数。但是有一个 move(toParent: )。

大多数语法在 Swift 3 中发生了变化,但他们没有删除任何 API 方法(如 CGPathMoveToPoint),只是重命名如下。

let path = CGMutablePath()
path.move(to: CGPoint(x: 10.0, y: 10.0))
path.addLine(to: CGPoint(x: 10.0, y: 10.0))