目标是圆角化类似于以下内容的非常规网格:

https://s-media-cache-ak0.pinimg.com/564x/50/bc/e0/50bce0cb908913ebc2cf630d635331ef.jpg

https://s-media-cache-ak0.pinimg.com/564x/7e/29/ee/7e29ee80e957ec22bbba630ccefbfaa2.jpg

这些栅格具有需要倒圆的多个角,而不是像常规栅格那样具有四个角的栅格。

蛮力方法是识别出带有角的瓷砖,然后用不同的背景图像或通过剪切代码中的角来围绕这些角。

有没有更清洁的方法?

在SpriteKit SKScene中为iOS应用渲染网格。

最佳答案

这是一个非常有趣的问题。您可以使用不同的方法来构建矩阵,但是每次您必须解决每个图块的背景4个角的变化时,都必须解决。

假设您从这样的GameViewController开始(不加载SKS文件,且anchorPoint等于零):

import UIKit
import SpriteKit
class GameViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        guard let view = self.view as! SKView? else { return }
        view.ignoresSiblingOrder = true
        view.showsFPS = true
        view.showsNodeCount = true
        let scene = GameScene(size:view.bounds.size)
        scene.scaleMode = .resizeFill
        scene.anchorPoint = CGPoint.zero
        view.presentScene(scene)
    }
}

我的想法是建立一个像这样的矩阵:
import SpriteKit
class GameScene: SKScene {
    private var sideTile:CGFloat = 40
    private var gridWidthTiles:Int = 5
    private var gridHeightTiles:Int = 6
    override func didMove(to view: SKView) {
        self.drawMatrix()
    }
    func drawMatrix(){
        var index = 1
        let matrixPos = CGPoint(x:50,y:150)
        for i in 0..<gridHeightTiles {
            for j in 0..<gridWidthTiles {
                let tile = getTile()
                tile.name = "tile\(index)"
                addChild(tile)
                tile.position = CGPoint(x:matrixPos.x+(sideTile*CGFloat(j)),y:matrixPos.y+(sideTile*CGFloat(i)))
                let label = SKLabelNode.init(text: "\(index)")
                label.fontSize = 12
                label.fontColor = .white
                tile.addChild(label)
                label.position = CGPoint(x:tile.frame.size.width/2,y:tile.frame.size.height/2)
                index += 1
            }
        }
    }
    func getTile()->SKShapeNode {
        let tile = SKShapeNode(rect: CGRect(x: 0, y: 0, width: sideTile, height: sideTile), cornerRadius: 10)
        tile.fillColor = .gray
        tile.strokeColor = .gray
        return tile
    }
}

输出:

ios - SpriteKit:关于非常规网格圆角的建议?-LMLPHP

现在我们可以为矩阵的每个图块构造一个背景。
我们可以制作相同的图块节点,但使用不同的颜色(可能比图块颜色更清晰)并且没有角半径。如果将背景分为四个部分,则可以得到:
  • 左-底部背景图块
  • 左-顶部背景图
  • 右-底部背景图
  • -顶部背景图

  • 典型背景图块的代码:
    func getBgTileCorner()->SKShapeNode {
       let bgTileCorner = SKShapeNode(rect: CGRect(x: 0, y: 0, width: sideTile/2, height: sideTile/2))
       bgTileCorner.fillColor = .lightGray
       bgTileCorner.strokeColor = .lightGray
       bgTileCorner.lineJoin = .round
       bgTileCorner.isAntialiased = false
       return bgTileCorner
    }
    

    现在使用SKSCropNode,我们可以使用背景图块和图块仅获取角点:
    func getCorner(at angle:String)->SKCropNode {
            let cropNode = SKCropNode()
            let tile = getTile()
            let bgTile = getBgTileCorner()
            cropNode.addChild(bgTile)
            tile.position = CGPoint.zero
            let tileFrame = CGRect(x: 0, y: 0, width: sideTile, height: sideTile)
            switch angle {
                case "leftBottom": bgTile.position = CGPoint(x:tile.position.x,y:tile.position.y)
                case "rightBottom": bgTile.position = CGPoint(x:tile.position.x+tileFrame.size.width/2,y:tile.position.y)
                case "leftTop": bgTile.position = CGPoint(x:tile.position.x,y:tile.position.y+tileFrame.size.height/2)
                case "rightTop": bgTile.position = CGPoint(x:tile.position.x+tileFrame.size.width/2,y:tile.position.y+tileFrame.size.height/2)
                default:break
            }
            tile.fillColor = self.backgroundColor
            tile.strokeColor = self.backgroundColor
            tile.lineWidth = 0.0
            bgTile.lineWidth = 0.0
            tile.blendMode = .replace
            cropNode.position = CGPoint.zero
            cropNode.addChild(tile)
            cropNode.maskNode = bgTile
            return cropNode
        }
    

    为典型角输出:
    let corner = getCorner(at: "leftBottom")
    addChild(corner)
    corner.position = CGPoint(x:50,y:50)
    

    ios - SpriteKit:关于非常规网格圆角的建议?-LMLPHP

    现在,我们可以使用每个图块的角来重建drawMatrix函数:
    func drawMatrix(){
            var index = 1
            let matrixPos = CGPoint(x:50,y:150)
            for i in 0..<gridHeightTiles {
                for j in 0..<gridWidthTiles {
                    let tile = getTile()
                    tile.name = "tile\(index)"
                    let bgTileLB = getCorner(at:"leftBottom")
                    let bgTileRB = getCorner(at:"rightBottom")
                    let bgTileLT = getCorner(at:"leftTop")
                    let bgTileRT = getCorner(at:"rightTop")
                    bgTileLB.name = "bgTileLB\(index)"
                    bgTileRB.name = "bgTileRB\(index)"
                    bgTileLT.name = "bgTileLT\(index)"
                    bgTileRT.name = "bgTileRT\(index)"
                    addChild(bgTileLB)
                    addChild(bgTileRB)
                    addChild(bgTileLT)
                    addChild(bgTileRT)
                    addChild(tile)
                    tile.position = CGPoint(x:matrixPos.x+(sideTile*CGFloat(j)),y:matrixPos.y+(sideTile*CGFloat(i)))
                    let label = SKLabelNode.init(text: "\(index)")
                    label.fontSize = 12
                    label.fontColor = .white
                    tile.addChild(label)
                    label.position = CGPoint(x:tile.frame.size.width/2,y:tile.frame.size.height/2)
                    bgTileLB.position = CGPoint(x:tile.position.x,y:tile.position.y)
                    bgTileRB.position = CGPoint(x:tile.position.x,y:tile.position.y)
                    bgTileLT.position = CGPoint(x:tile.position.x,y:tile.position.y)
                    bgTileRT.position = CGPoint(x:tile.position.x,y:tile.position.y)
                    index += 1
                }
            }
    }
    

    输出:

    ios - SpriteKit:关于非常规网格圆角的建议?-LMLPHP

    与屏幕截图非常相似(这是两个图块示例:)

    ios - SpriteKit:关于非常规网格圆角的建议?-LMLPHP

    ios - SpriteKit:关于非常规网格圆角的建议?-LMLPHP

    现在,当您要删除一个图块时,您可以决定要删除或离开哪个角,因为对于每个图块,您还具有相对的四个角:

    输出:

    ios - SpriteKit:关于非常规网格圆角的建议?-LMLPHP

    关于ios - SpriteKit:关于非常规网格圆角的建议?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43463972/

    10-13 03:59