Hi.
I am trying to add background with gradient color to my SKScene:
class GameScene: SKScene
{
override func didMove(to view: SKView)
{
let gradientView = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
let image = UIImage.gradientBackgroundImage(bounds: CGRect(x: 0, y: 0, width: frame.width, height: frame.height),
colors: [UIColor.yellow.cgColor, UIColor.blue.cgColor])
let background = SKSpriteNode(color: UIColor(patternImage: image), size: frame.size)
addChild(background)
}
}
extension UIImage
{
/**
http://www.riptutorial.com/ios/example/14328/gradient-image-with-colors
*/
static func gradientBackgroundImage(bounds: CGRect, colors: [CGColor]) -> UIImage
{
let gradientLayer = CAGradientLayer()
gradientLayer.frame = bounds
gradientLayer.colors = colors
UIGraphicsBeginImageContext(gradientLayer.bounds.size)
gradientLayer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}
This is not working. Is there a better way to add gradient background to the SKScene?
Thanks a lot!
EDIT:
If I do this in didMove(to view:):
self.view?.backgroundColor = UIColor.blue
let backgroundDep = IGABackgroundLayer().gradientMetar() // My method to create CAGradientLayer
backgroundDep.frame = self.view!.bounds
self.view!.layer.insertSublayer(backgroundDep, at: 0)
...background is created nicely but added SKNodes are not seen on top of it.