我打算用CoreGraphics绘制像图形一样的指南针。我有以下代码:
// Set origin point
context?.saveGState()
context?.translateBy(x: centreX, y: centreY)
context?.setLineWidth(2.0)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let components: [CGFloat] = [1.0, 1.0, 1.0, 1.0]
let color = CGColor(colorSpace: colorSpace, components: components)
context?.setStrokeColor(color!)
for degrees:Double in stride(from: 0, to: 355, by: 5) {
NSLog("Degrees: \(degrees)")
context?.move(to: CGPoint(x: 0, y: -100))
context?.addLine(to: CGPoint(x: 0, y: -80))
context?.strokePath()
// Rotate the context
context?.rotate(by: CGFloat(M_PI * degrees / 180))
}
// Restore the context
context?.restoreGState()
我的问题是并非所有的线都被绘制出来。我已经验证它正确地完成了循环,NSLog显示了所有需要的5度增量。但并非所有这些线都被绘制出来。
有什么建议吗?