Swift 特殊圆角-SKShapeNode

Swift 特殊圆角-SKShapeNode,swift,sprite-kit,skshapenode,Swift,Sprite Kit,Skshapenode,我有一个SKShapeNode,如果满足特定条件,它需要将每个角都圆角。阅读下面链接中提供的答案,这似乎非常简单,因为我只需使用|=来舍入需要舍入的加法角(4x if语句) 然而,这是行不通的!当我使用下面的代码时,我得到错误消息“Binart运算符'|='不能应用于两个'UIRectCorner'操作数” 或 我一定是做错了什么,但我不知道是什么?任何帮助都将不胜感激 解决了我的问题。|=和+=不起作用,但=[previousValue,newValue]似乎起作用。下面是我的代码。如果有更

我有一个SKShapeNode,如果满足特定条件,它需要将每个角都圆角。阅读下面链接中提供的答案,这似乎非常简单,因为我只需使用|=来舍入需要舍入的加法角(4x if语句)

然而,这是行不通的!当我使用下面的代码时,我得到错误消息“Binart运算符'|='不能应用于两个'UIRectCorner'操作数”


我一定是做错了什么,但我不知道是什么?任何帮助都将不胜感激

解决了我的问题。|=和+=不起作用,但=[previousValue,newValue]似乎起作用。下面是我的代码。如果有更好的办法,请告诉我

func roundCorners() {

    let TR = true
    let TL = true
    let BR = false
    let BL = true

    var corners: UIRectCorner = []

    if TR == true {
        corners = [corners, .topRight]
    }

    if TL == true {
        corners = [corners, .topLeft]
    }

    if BR == true {
        corners = [corners, .bottomRight]
    }

    if BL == true {
        corners = [corners, .bottomLeft]
    }

    let rect = CGRect(x: -50, y: -50, width: 100, height: 100)
    let cornerSize = CGSize(width: 10, height: 10)

    let shape = SKShapeNode()
    shape.fillColor = UIColor.black
    shape.path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: cornerSize).cgPath
    addChild(shape)

}

我写了一个非常方便的扩展,我在所有的SpriteKit游戏中都大量使用它。它将获取现有的SKShapeNode并复制其所有相关属性,然后删除并创建一个新的属性,并使用指定的圆角。注意:如果形状节点有任何子节点,则不应使用此选项,因为它们不会在新创建过程中保持不变。因此,在添加任何子项之前,请始终使用此方法

shapeNode.roundCorners(topLeft:true,topRight: true,bottomLeft:false,bottomRight:false,radius:20,parent:self)


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
        newNode.fillColor = self.fillColor
        newNode.strokeColor = self.strokeColor
        newNode.fillTexture = self.fillTexture
        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
    }
}
Swift 5.2.x:
用法:
很抱歉迟了答复。这看起来很棒!但我在“if topLeft”行得到一个错误,“Expected expression in container literal”。你知道我为什么会犯这个错误吗?对不起,之前有个打字错误。它应该以<代码> uiRealCordNeNe()结束:<代码> > <代码> UIRectCorner([< /代码>)。如果您觉得这帮助或解决了问题,请考虑将投票或标记作为答案。很高兴我能提供帮助。
func roundCorners() {

    let TR = true
    let TL = true
    let BR = false
    let BL = true

    var corners: UIRectCorner = []

    if TR == true {
        corners = [corners, .topRight]
    }

    if TL == true {
        corners = [corners, .topLeft]
    }

    if BR == true {
        corners = [corners, .bottomRight]
    }

    if BL == true {
        corners = [corners, .bottomLeft]
    }

    let rect = CGRect(x: -50, y: -50, width: 100, height: 100)
    let cornerSize = CGSize(width: 10, height: 10)

    let shape = SKShapeNode()
    shape.fillColor = UIColor.black
    shape.path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: cornerSize).cgPath
    addChild(shape)

}
shapeNode.roundCorners(topLeft:true,topRight: true,bottomLeft:false,bottomRight:false,radius:20,parent:self)


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
        newNode.fillColor = self.fillColor
        newNode.strokeColor = self.strokeColor
        newNode.fillTexture = self.fillTexture
        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
    }
}
extension SKShapeNode {
    convenience init(corners:UIRectCorner, size:CGSize, radius:CGFloat) {
        // flips corners vertically for UIBezierPath and SKSpriteKit
        var flipCorners = UIRectCorner()
        flipCorners = corners.contains(.topLeft) ? flipCorners.union(.bottomLeft): flipCorners
        flipCorners = corners.contains(.topRight) ? flipCorners.union(.bottomRight): flipCorners
        flipCorners = corners.contains(.bottomLeft) ? flipCorners.union(.topLeft): flipCorners
        flipCorners = corners.contains(.bottomRight) ? flipCorners.union(.topRight): flipCorners
        self.init(path: UIBezierPath.init(roundedRect: CGRect(origin:CGPoint.zero,size:size), byRoundingCorners: flipCorners, cornerRadii: CGSize(width: radius, height: radius)).cgPath)
    }
}
let roundedShape = SKShapeNode(corners: UIRectCorner([.topLeft,.topRight]), size: CGSize(width:200,height:100), radius: 16)