0

我有一个 UIView,我用 UIBezierPath() 画线,我有 SKSHapeNode 孩子。有时,绘图与节点叠加。在这种情况下,我希望节点位于绘图的上方。但是即使节点有更大的 Zpos,绘图也是向上的节点。

当我想画一条线时,我使用这个脚本:

private func addLine(fromPoint start: CGPoint, toPoint end:CGPoint) {
    let line = CAShapeLayer()
    let linePath = UIBezierPath()
    linePath.move(to: start)
    linePath.addLine(to: end)
    line.zPosition = 80
    line.path = linePath.cgPath
    line.strokeColor = UIColor.black.cgColor
    line.lineWidth = 5.0
    line.lineJoin = CAShapeLayerLineJoin.round
    self.view!.layer.addSublayer(line)
}

我将此脚本用于 SKSHapeNodes :

private func drawBase(map : Int) {
    for i in 0...maps.xMaps[map].count - 1 {
        let team = maps.mapTeam[map][i]
        let weight = CGFloat(game!.baseWeight[i])
        let base = SKShapeNode(rectOf: CGSize(width: weight, height: weight))
        base.zPosition = 90
        base.position = CGPoint(x: maps.xMaps[map][i], y: maps.yMaps[map][i])

        if team == 0 {
            base.fillColor = .blue
        } else if team == 1 {
            base.fillColor = .red
        } else if team == 2 {
            base.fillColor = .green
        } else if team == 3 {
            base.fillColor = .purple
        }
        mapEntity.append(base)
        self.addChild(base)
    }
}

我在这里调用函数:

func drawMap(map : Int){
    drawRoad(map: map)
    drawBase(map: map)
}

DrawRoad 是画线的函数:

private func drawRoad(map : Int) {
    for i in 0...maps.xRoad[map].count - 1 {
        let start = CGPoint(x: maps.xRoad[map][i][0], y: self.frame.height - maps.yRoad[map][i][0])
        let end = CGPoint(x: maps.xRoad[map][i][1], y: self.frame.height - maps.yRoad[map][i][1])
        addLine(fromPoint: start, toPoint: end)
    }
}

如您所见,我在添加 SKShapeNode 之前画了线,但它不起作用...

4

0 回答 0