我有一个SKShapeNode,如果满足特定条件,它需要将每个角的四舍五入。阅读下面的链接提供的答案,这似乎很容易安静,因为我只是使用| =来舍入需要舍入的额外角(4x if语句)。
How to write a generic UIRectCorner function?
然而,这不起作用!当我使用下面的代码时,我收到错误消息" Binart operator' | ='不能适用于两个UIRectCorner'操作数"
var corners: UIRectCorner = UIRectCorner(rawValue: 0) | UIRectCorner(rawValue: 1)
或
var corners: UIRectCorner = UIRectCorner(rawValue: 0)
corners |= UIRectCorner(rawValue: 1)
我一定是做错了什么,但我无法弄明白什么?任何帮助将非常感激。
答案 0 :(得分:2)
我写了一个非常方便的扩展,我在所有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
}
}
答案 1 :(得分:1)
排序解决了我的问题。 | =和+ =没有工作,但= [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)
}
答案 2 :(得分:1)
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)