长话短说,我想在 CALayerShape 上画一个对角线网格。我的代码而不是直线产生某种弯曲的疯狂。它看起来很有趣,我希望它能达到预期的效果,但不,不是。
我的问题: 1. 我如何使用 CAShapeLayer 绘制直线? 2. 我是否发现了某种影响模拟器中像素引力的黑洞?
func drawPath(_view:UIView, phase:CGFloat, segment:CGFloat){
// we need two runs
var height = CGRectGetHeight(view.frame)
let width = CGRectGetWidth(view.frame)
let segment:CGFloat = 40
var currentX:CGFloat = segment
var cgpath = CGPathCreateMutable()
for var y = height; y > 0 ; y = y - segment {
CGPathMoveToPoint(cgpath, nil, 0, y)
CGPathAddLineToPoint(cgpath, nil, currentX, 0)
CGPathCloseSubpath(cgpath)
currentX += segment
}
var layer = CAShapeLayer()
layer.path = cgpath
layer.strokeColor = UIColor.redColor().CGColor
layer.lineWidth = 1
layer.fillColor = UIColor.blackColor().CGColor
view.layer.addSublayer(layer)
}
最佳答案
你画的实际上是直线。它看起来弯曲的事实是一种错觉。你可以用 yarn 和一 block 硬纸板获得同样的效果。
您的代码做错了事。
您需要用方格纸绘制出您想要的对角线网格,找出对角线网格的端点,然后想出一个循环来为您提供这些端点。
如果您想在 5x5 矩形中绘制对角线网格,您可以像这样绘制线条:
0,1 to 1,0
0,2 to 2,0
0,3 to 3,0
0,4 to 4,0
您的网格间距可能比每个点都多 20 或 30 点,并且您的整个网格可能是屏幕的全尺寸,但您明白了。
关于ios - 使用 CAShapeLayer 绘制网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31529640/