首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带SKShapeNode的圆形矩形

带SKShapeNode的圆形矩形
EN

Stack Overflow用户
提问于 2017-03-02 18:45:54
回答 1查看 2.1K关注 0票数 3

我有下面的代码,在场景生成器中绕过一个已经存在的矩形.

代码语言:javascript
复制
btOk.path = UIBezierPath(roundedRect: CGRect(x: -62.5,y:-25,width: 125, height: 50), cornerRadius: 10).cgPath

但是,如果我尝试使用相同的init来绕过另一个更大的矩形的角,它甚至不会接近工作。它只是使宽度胡UUGE(想象特朗普)。

代码语言:javascript
复制
scene.enumerateChildNodes(withName: "//*"){
            node,stop in

    if let name = node.name{
        if name.contains("round"){
            if let shapeNode = node as? SKShapeNode{
                print(shapeNode.frame.width) //500
                shapeNode.path = UIBezierPath(roundedRect: CGRect(x: -250,y:-100,width: 500, height: 200), cornerRadius: 50).cgPath
                print(shapeNode.frame.width) //5735
             }
        }
    }
}

btOk也是SKShapeNode。我在这两个人之间错过了什么?有一点要注意的是,我像这样通过场景的子类来枚举,因为这个场景在SKReferenceNode中。也许这和这有什么关系?

编辑

在@Ron的指导下,我已经解决了这个问题,因为它是一个皮塔,我还创建了一个扩展。因此,在需要的时候,我可以很容易地绕过任何SKShapeNode的角落。即使它是在场景编辑器中创建的。注意,只有在没有形状节点的子节点的情况下,才应该使用此方法。否则,这些孩子也将被移走。

代码语言:javascript
复制
extension SKShapeNode {
    func roundCorners(topLeft:Bool,topRight:Bool,bottomLeft:Bool,bottomRight:Bool,radius: CGFloat,parent: SKNode){
        let newNode = SKShapeNode(rect: self.frame)
        newNode.fillColor = self.fillColor
        newNode.lineWidth = self.lineWidth
        newNode.position = self.position
        newNode.name = self.name
        self.removeFromParent()
        parent.addChild(newNode)
        var corners = UIRectCorner()
        if topLeft { corners = corners.union(.bottomLeft) }
        if topRight { corners = corners.union(.bottomRight) }
        if bottomLeft { corners = corners.union(.topLeft) }
        if bottomRight { corners = corners.union(.topRight) }
        newNode.path = UIBezierPath(roundedRect: CGRect(x: -(newNode.frame.width / 2),y:-(newNode.frame.height / 2),width: newNode.frame.width, height: newNode.frame.height),byRoundingCorners: corners, cornerRadii: CGSize(width:radius,height:radius)).cgPath
    }
}

像这样使用它..。

代码语言:javascript
复制
aShapeNode.roundCorners(topLeft: true, topRight: true, bottomLeft: false, bottomRight: false, radius: 5,parent: popup)
EN

回答 1

Stack Overflow用户

发布于 2017-03-02 19:49:15

这不是您想要听到的,而是因为您不能在场景编辑器中设置SKShapeNode的宽度(据我所知)。为了使ShapeNode具有500个宽度,您必须调整xScale。然后,当您调整路径时,xScale会重新应用到路径(某种程度上是指数增长)。如果您在代码中创建SKShapeNode,那么调整圆角没有问题。

代码语言:javascript
复制
    let round = SKShapeNode(rect: CGRect(x: 0, y: -150, width: 500, height: 200))
    round.fillColor = .red
    addChild(round)
    print(round.frame.width)
    round.path = UIBezierPath(roundedRect: CGRect(x: -250, y: -100, width: 500, height: 200), cornerRadius: 50).cgPath
    print(round.frame.width)

编辑

如果您有决心使用场景编辑器,您可以将您的ShapeNode放在您想要的地方并将其扩展到您想要的地方,那么您就可以在代码中进行一个小的转换,以获得您想要的结果。

代码语言:javascript
复制
    if let round = self.childNode(withName: "biground") as? SKShapeNode {

        let biground = SKShapeNode(rect: round.frame)
        biground.fillColor = round.fillColor
        biground.position = round.position
        addChild(biground)
        round.removeFromParent()

        print(biground.frame.width)
        biground.path = UIBezierPath(roundedRect: CGRect(x: -250, y: -100, width: 500, height: 200), cornerRadius: 50).cgPath
        print(biground.frame.width)
    }

这只是根据场景编辑器中所勾画的内容在代码中重新创建形状,并完美地绕过边缘。

编辑2

我一直觉得SKShapeNodes效率很低(我很确定他们也泄露了内存)。所以我总是把我的圆形矩形设置成这样。

代码语言:javascript
复制
    let outsideTexture = SKTexture(imageNamed: "round_square_white")
    let outside = SKSpriteNode(texture: outsideTexture)      
    let insetX: CGFloat = 20
    let insetY: CGFloat = 20
    let cellSize = CGSize(width: 500.0, height: 500.0)

    outside.centerRect = CGRect(x: CGFloat(insetX / outside.size.width), y: CGFloat(insetY / outside.size.height), width: CGFloat((outside.size.width - insetX * 2) / outside.size.width), height: CGFloat((outside.size.height - insetY * 2) / outside.size.height))
    //outside.position = CGPointMake(gameModel.gameWidth / 2, (outside.size.height) / 2);
    outside.xScale = cellSize.width / outside.size.width
    outside.yScale = cellSize.height / outside.size.height
    outside.zPosition = 10
    outside.position = CGPoint(x: CGFloat(0), y: CGFloat(-0.35 * self.size.height / 2))
    self.addChild(outside)

值得注意的是,这布局了一个圆形的正方形/矩形,但是与场景编辑器中的缩放问题非常相似,您必须在上面放置一个空单元格来添加子单元格,否则它们就会缩放到圆形的方形。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42563465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档