画一条改变方向的线

时间:2023-01-23 14:36:51

I currently am attempting to make an app that consists of a line that goes straight up, but when the screen is tapped it changes at a 45 degree angle (To the left or right).

我目前正在尝试制作一个由直线组成的应用程序,但当屏幕被点击时,它会以45度角(向左或向右)改变。

I'm creating the path for this like so:

我为它创建了这样的路径:

        var path = CGPathCreateMutable()
    for var i = 0; i < wayPoints.count; i++ {
        let p = wayPoints[i]
        if i == 0 {
            CGPathMoveToPoint(path, nil, p.x, p.y)
        } else {
            CGPathAddLineToPoint(path, nil, p.x, p.y)
        }
    }

And I populate the wayPoints array like so (This chunk of code is called a few times every second):

然后我就像这样填充了wayPoints数组(这段代码每秒钟被调用几次):

        if (wayPoints.isEmpty) {
        wayPoints.append(CGPointMake(0, CGRectGetMinY(self.view!.frame)))
    } else {
        if (wayPoints.count >= 30) {
            while (wayPoints.count >= 30) {
                wayPoints.removeAtIndex(0)
            }
        }
        if (currentPosition == Position.Right) {
            wayPoints.append(CGPointMake(wayPoints.last!.x + 1, wayPoints.last!.y + 1))
        } else if (currentPosition == Position.Left) {
            wayPoints.append(CGPointMake(wayPoints.last!.x - 1, wayPoints.last!.y + 1))
        } else {
            wayPoints.append(CGPointMake(0, wayPoints.last!.y + 1))
        }
    }

(The currentPosition changes when a tap occurs).

(当发生抽头时,当前位置发生变化)。

And although I don't think this matters, this is how I'm creating the SKShapeNode line:

虽然我不认为这很重要,但这就是我创建SKShapeNode线的方式:

let shapeNode = SKShapeNode()
shapeNode.path = path
shapeNode.name = "line"
shapeNode.strokeColor = UIColor.blackColor()

This somewhat works, but for some reason it seems as if there is a "leak" (not memory) in the path. This is what it looks like with the above code: http://gyazo.com/92dd84de15e125ea3d598cbfa9a86b13

这在某种程度上行得通,但出于某种原因,似乎路径中存在“泄漏”(而不是内存)。上面的代码是这样的:http://gyazo.com/92dd84de15e125ea3d598cbfa9a86b13

As you can see, it makes almost a triangle when the line turns. It should just be a line without all that excess 'mass' on the sides.

正如你所看到的,当换行时,它几乎变成一个三角形。它应该是一条线,两边没有多余的质量。

1 个解决方案

#1


1  

The triangle you see is the line path being filled with a color. In this case, black. To remedy this, set the fillColor property of the shape node to the invisible color by

你看到的三角形是用颜色填充的线路径。在这种情况下,黑色的。为此,将形状节点的fillColor属性设置为不可见颜色

shapeNode.fillColor = SKColor.clearColor()

I used SKColor here instead of UIColor because the former can be used, without modification, in both Mac OS X and iOS apps.

我在这里使用SKColor而不是UIColor,因为前者可以在Mac OS X和iOS应用程序中不加修改地使用。

#1


1  

The triangle you see is the line path being filled with a color. In this case, black. To remedy this, set the fillColor property of the shape node to the invisible color by

你看到的三角形是用颜色填充的线路径。在这种情况下,黑色的。为此,将形状节点的fillColor属性设置为不可见颜色

shapeNode.fillColor = SKColor.clearColor()

I used SKColor here instead of UIColor because the former can be used, without modification, in both Mac OS X and iOS apps.

我在这里使用SKColor而不是UIColor,因为前者可以在Mac OS X和iOS应用程序中不加修改地使用。