圆形特定角 - SKShapeNode
Round Specific Corners - SKShapeNode
我有一个 SKShapeNode,如果满足特定条件,它需要将每个角都圆化。阅读下面 link 提供的答案,这似乎很容易,因为我只需使用 |= 来圆角加法角需要四舍五入(4x if 语句)。
但是,这是行不通的!当我使用下面的代码时,我收到错误消息 "Binart operator '|=' cannot be applied to two 'UIRectCorner' operands"
var corners: UIRectCorner = UIRectCorner(rawValue: 0) | UIRectCorner(rawValue: 1)
或
var corners: UIRectCorner = UIRectCorner(rawValue: 0)
corners |= UIRectCorner(rawValue: 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)
}
我写了一个非常方便的扩展,我在我的所有 SpriteKit 游戏中大量使用它。它采用现有的 SKShapeNode 并复制其所有相关属性,然后删除并创建一个新的,无论您指定要圆角的角。注意:如果形状节点有任何 children,则不应使用它,因为它们不会在新创建过程中持续存在。所以在添加任何 children 之前总是使用这个方法。
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:
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)
我有一个 SKShapeNode,如果满足特定条件,它需要将每个角都圆化。阅读下面 link 提供的答案,这似乎很容易,因为我只需使用 |= 来圆角加法角需要四舍五入(4x if 语句)。
但是,这是行不通的!当我使用下面的代码时,我收到错误消息 "Binart operator '|=' cannot be applied to two 'UIRectCorner' operands"
var corners: UIRectCorner = UIRectCorner(rawValue: 0) | UIRectCorner(rawValue: 1)
或
var corners: UIRectCorner = UIRectCorner(rawValue: 0)
corners |= UIRectCorner(rawValue: 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)
}
我写了一个非常方便的扩展,我在我的所有 SpriteKit 游戏中大量使用它。它采用现有的 SKShapeNode 并复制其所有相关属性,然后删除并创建一个新的,无论您指定要圆角的角。注意:如果形状节点有任何 children,则不应使用它,因为它们不会在新创建过程中持续存在。所以在添加任何 children 之前总是使用这个方法。
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:
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)