Use a factory to make a shadow:
import SpriteKit
class MAKE {
private static let view:SKView = SKView()
static func makeShadow(from source: SKTexture, rgb: SKColor, a: CGFloat) -> SKSpriteNode {
let shadowNode = SKSpriteNode(texture: source)
shadowNode.colorBlendFactor = 0.5 // makes the following line more effective
shadowNode.color = SKColor.gray // makes for a darker shadow. Off for "glow" shadow
let textureSize = source.size()
let doubleTextureSize = CGSize(width: textureSize.width * 2, height: textureSize.height * 2)
let framer = SKSpriteNode(color: UIColor.clear, size: doubleTextureSize)
framer.addChild(shadowNode)
let filter = CIFilter(name: "CIGaussianBlur")
let blurAmount = 10
filter?.setValue(blurAmount, forKey: kCIInputRadiusKey)
let effectsNode = SKEffectNode()
effectsNode.filter = filter
effectsNode.blendMode = .alpha
effectsNode.addChild(framer)
effectsNode.shouldRasterize = true
let tex = view.texture(from: effectsNode)
let shadow = SKSpriteNode(texture: tex)
shadow.colorBlendFactor = 0.5
shadow.color = rgb
shadow.alpha = a
shadow.zPosition = -1
return shadow
}
}
Now make a button anyway you like, but make sure you have created a buttonTexture
that's the exact same size and shape as your button, preferably a grey rendition of it. You'll need this to send to the blurring factory shadowmaker above, like this:
shadowSprite = MAKE.makeShadow(from: buttonTexture, rgb: myColor, a: 0.33)
shadowSprite.position = CGPoint(x: self.frame.midX, y: self.frame.midY - 5)
addChild(shadowSprite)
You can see more details on how and why a factory, here:
Create \(Use) SKView as \(in a) factory \(static class)