Blend Mode between NSWindow and CAShapeLayer - swift

I'm trying to make a semi-transparent window (white), and have a view that sits over the top of it to make it appear clear again.
Essentially, it looks like:
And what I want it to look like is:
My window is defined like:
window!.alphaValue = 0;
// Set level to screensaver level
window!.level = 1000
window!.backgroundColor = NSColor.white;
window!.animator().alphaValue = 0.5;
A white window with 50% opacity. The view on top contains a single CAShapeLayer. This layer is defined like:
shapeLayer = CAShapeLayer();
shapeLayer!.lineWidth = 1.0;
shapeLayer!.strokeColor = NSColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1).cgColor;
shapeLayer!.fillColor = NSColor(red: 0, green: 0, blue: 0, alpha: 0.5).cgColor;
This window is movable by the user, so I need some kind of dynamic way to clear the contents of the main window and show through to whatever is underneath (the desktop, another application, etc)

Related

Best practise for managing UI in SpriteKit

In SpriteKit, I'm using SKNodes, SKShapeNodes and SKLabelNodes for all my UI. It works well for the small game I've made, however, one small interface such as a 'Game Over' prompt can consist of well over 100 lines of code, and that's merely declaring and defining node properties. All these prompts and other UI are declared in their own separate functions and are called when needed throughout the Game Scene, but I can't shake the feeling that there is a better practise to managing UI. Perhaps a class where I can call the 'getGameOverPromptUI' or 'getScoreLabel' functions? Would I limit such a class to only the Game Scene? or include UI for the Menu Scene?
Edit:
To clarify, a Game Over prompt consists of a faded background covering the scene, a rectangle as the prompt box behind the text, labels for the game over title, the current score, the highest score, a button for playing again, a button for going back to the menu etc.
The reason for the code being so large is every component is pretty much created like this
let gameOverNode = SKNode()
gameOverNode.position = CGPoint(x: 0, y: screenSize.height / 16)
addChild(gameOverNode)
// Faded Background
let fadedBackgroundNode = SKShapeNode(rectOf: screenSize)
fadedBackgroundNode.fillColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.6)
fadedBackgroundNode.zPosition = 1
fadedBackgroundNode.position = CGPoint(x: -gameOverNode.position.x, y: -gameOverNode.position.y)
gameOverNode.addChild(fadedBackgroundNode)
// Prompt Background
let gameOverGradientNode = SKShapeNode(rectOf: gameOverGradientNodeSize, cornerRadius: gameOverGradientCornerRadius)
gameOverGradientNode.fillColor = .white
gameOverGradientNode.strokeColor = .clear
let gameOverCropNode = SKCropNode()
gameOverCropNode.maskNode = gameOverGradientNode
gameOverCropNode.addChild(helper.getBackgroundNode2(nodeSize: gameOverGradientNode.frame.size))
gameOverCropNode.zPosition = 2
gameOverNode.addChild(gameOverCropNode)
// Title Label
let gameOverLabel = SKLabelNode(fontNamed: "")
gameOverLabel.name = "gameOverLabel"
gameOverLabel.text = "Game Over"
gameOverLabel.position = CGPoint(x: 0, y: gameOverGradientNode.frame.height / 4 + gameOverGradientNode.frame.height / 8)
gameOverLabel.fontSize = 160
gameOverLabel.fontColor = .white
gameOverLabel.zPosition = 3
gameOverLabel.numberOfLines = 2
gameOverLabel.horizontalAlignmentMode = .center
gameOverNode.addChild(gameOverLabel)

Use GMSCircle as hole of a polygon instead of GMSPath

I have a polygon shape with a square hole punched out, I've been fiddling with trying to do the same but with a circle, GMSCircle, but haven't had any luck.
Would anyone know the proper way to do it?
Here is my current code block for the polygon hole.
let polygon = GMSPolygon()
polygon.path = GMSPath(path: northAmericaPolygon)
polygon.holes = [GMSPath(path: sanFranciscoHole)]
polygon.fillColor = UIColor(displayP3Red: 255, green: 0, blue: 0, alpha: 0.2)
polygon.strokeColor = .black
polygon.strokeWidth = 2
polygon.map = mapView

Translucent hole with center text in PieChartView

How to create a translucent hole and show center text together?I want to draw an entire translucent hole instead of a part. At the same time, I need to draw the center text
Charts
I have tried to show translucent hole using follow
// draw the hole
chartView.drawHoleEnabled = true
chartView.holeRadiusPercent = 0
chartView.transparentCircleRadiusPercent = 0.382
chartView.transparentCircleColor = UIColor(red: 0.7, green: 0.7, blue: 0.7, alpha: 0.5)
// draw the center text
chartView.drawCenterTextEnabled = true
chartView.centerText = "SUM of\n3,909.00"
but when set the holeRadiusPercent is zero, the center text would not draw in the hole
how could i do it?

How do you fade the bottom of an image into the background? [duplicate]

I've read examples around here but I cannot do it the way I desire, somehow my gradient examples are stuck on the middle of the screen, not working as expected.
I have a UICollectionView which fills the whole screen with vertical scroll.
I want the top and bottom of the UICollectionView to be black and the middle to be transparent (since I'm using a black blackgroundColor).
I tried to apply gradients, but somehow I'm failing to accomplish what I want.
Here's my code:
let gradient = CAGradientLayer()
gradient.frame = view.bounds
gradient.colors = [UIColor.black.cgColor, UIColor.clear.cgColor, UIColor.black.cgColor]
gradient.startPoint = CGPoint(x: 1, y: 0)
gradient.endPoint = CGPoint(x: 1, y: 1)
view.layer.mask = gradient
The code above is placing the gradient in the middle of the screen but inverted. It is transparent on top and bottom of the screen and the middle section if fading to complete black.
I'm trying to create something like this:
Thanks for helping
You can achieve that by changing your color line with this:
gradient.colors = [
UIColor(white: 1.0, alpha: 0).cgColor,
UIColor(white: 1.0, alpha: 1).cgColor,
UIColor(white: 1.0, alpha: 0).cgColor
]
Or if you want to have even more control over your gradient, you can also use the code below and play around with the location and/or color alpha values:
let gradient = CAGradientLayer()
gradient.frame = view.bounds
gradient.colors = [
UIColor(white: 1, alpha: 0).cgColor,
UIColor(white: 1, alpha: 1).cgColor,
UIColor(white: 1, alpha: 1).cgColor,
UIColor(white: 1, alpha: 0).cgColor
]
gradient.locations = [0, 0.4, 0.6, 1]
view.layer.mask = gradient
Reason of this from the documentation;
An optional layer whose alpha channel is used to mask the layer’s content.
The layer’s alpha channel determines how much of the layer’s content and background shows through. Fully or partially opaque pixels allow the underlying content to show through but fully transparent pixels block that content.

How to fill color outside the circle on google map using Swift?

I want the red circle just have the border (no red in centre) and have the rest outside the circle darkened?
I have a issue about that. Could you help me solve it?
With google maps for this, you should do the following steps
Create a GMSCircle object
Set stroke color and fill color for the
circle
Set map for the circle object
Example code:
let circleCenter = CLLocationCoordinate2DMake(9.931233, 76.267303)//change to your center point
let circ = GMSCircle(position: circleCenter, radius: 5000)//radius in meters
circ.fillColor = UIColor(red: 0.35, green: 0, blue: 0, alpha: 0.05)
circ.strokeColor = UIColor.redColor()
circ.strokeWidth = 1
circ.map = mapView //your map view in the UI