快速提问:此代码应产生(以及其他)4条彼此垂直的线。但是,在运行时,线条都会少量关闭。
for i in 0..<60 {
CGContextSaveGState(ctx)
CGContextTranslateCTM(ctx, rect.width / 2, rect.height / 2)
CGContextRotateCTM(ctx, CGFloat(6.0 * Double(i) * M_PI / 180))
CGContextSetStrokeColorWithColor(ctx, UIColor.grayColor().CGColor)
CGContextMoveToPoint(ctx, 50, 50)
if (i % 5 == 0) {
CGContextSetLineWidth(ctx, 3.0)
CGContextAddLineToPoint(ctx, 30, 30)
}
else {
CGContextSetLineWidth(ctx, 2.0)
CGContextAddLineToPoint(ctx, 40, 40)
}
CGContextStrokePath(ctx)
CGContextRestoreGState(ctx)
}
还在学习核心图形,如果这很简单就很抱歉。
问候,布兰登
编辑1:
这是我得到的:
答案 0 :(得分:2)
如果我正确地假设它,你想在表盘上绘制看起来像刻度线的刻度线。
如果你让它只画一次迭代,例如
for i in 0..<1
您将看到第一个刻度以某个角度开始。发生这种情况是因为您从(x = 50,y = 50)到(x = 30,y = 30)
绘制线条尝试将其更改为(x = 50,y = 0)(x = 30,y = 0)
for i in 0..<60 {
CGContextSaveGState(ctx)
CGContextTranslateCTM(ctx, rect.width / 2, rect.height / 2)
CGContextRotateCTM(ctx, CGFloat(6.0 * Double(i) * M_PI / 180))
CGContextSetStrokeColorWithColor(ctx, UIColor.grayColor().CGColor)
CGContextMoveToPoint(ctx, 50, 0) // <-- changed here
if (i % 5 == 0) {
CGContextSetLineWidth(ctx, 3.0)
CGContextAddLineToPoint(ctx, 30, 0) // <-- changed here
}
else {
CGContextSetLineWidth(ctx, 2.0)
CGContextAddLineToPoint(ctx, 40, 0) // <-- changed here
}
CGContextStrokePath(ctx)
CGContextRestoreGState(ctx)
}
结果