How to fill color outside the circle on google map using Swift? - 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

Related

How to draw gradient in SKShapeNode?

I am trying to draw gradient in SKShapeNode object.
I draw a triangle with a color using alpha component and it works fine. And I would like to add gradient so one edge of the triangle disappears slowly to the background. What I want to achieve is to simulate sight range of a character.
I have found one answer regarding this challenge: How to apply a gradient to SKShapeNode created from a path, but the answer doesn't work in XCode 13.1.
The reason is that proposed solution of adding SKTexture can't be compiled:
var testTexture = SKTexture(size: CGSize(width: 200, height: 1), color1: CIColor(red: 1.0, green: 0.0, blue: 1.0, alpha: 1.0), color2: CIColor(red: 1.0, green: 0.0, blue: 1.0, alpha: 0.0), direction: GradientDirection.Left)
It results in the error "Cannot find 'GradientDirection' in scope...". And I cannot find any other method to add gradient.
I have found another answer which says that it isn't possible, but it is from 2013, so maybe something changed (How to create a Gradient in Spritekit?).
I also tried this solution: https://augmentedcode.io/2017/11/12/drawing-gradients-in-spritekit/, but it doesn't produce any effect and decreases performance of the application.
Some sources from the internet directed my to OpenGL Shading Language: https://www.khronos.org/opengl/wiki/Core_Language_(GLSL).
Here is partly solution:
let sight = SKShapeNode()
let gradientShader = SKShader(source: "void main() {" +
"float distanceFromCenter = distance(v_tex_coord, vec2(0.5,0.5));" +
"gl_FragColor = vec4(0.6, 0.3, 0.0, distanceFromCenter*6.0);" +
"}")
sight.fillColor = .clear
sight.fillShader = gradientShader
Here are helpful links: Drawing a circle with a shader in SpriteKit, https://thebookofshaders.com/02/, Add shader for SKShapeNode, https://developer.apple.com/documentation/spritekit/skshapenode/controlling_shape_drawing_with_shaders.
The above solution was half way, but after another couple of hours I managed to achieve what I wanted to.
let pos = n.sprite.position
let frame = n.sight.frame
let x = (pos.x - frame.minX) / frame.width
let y = (pos.y - frame.minY) / frame.height
let gradientShader = SKShader(source: "void main() {" +
"float distanceFromCenter = distance(v_tex_coord, npcLocation);" +
"float alpha = 0.8 - distanceFromCenter;" +
"if (alpha < 0) { alpha = 0.0;}" +
"gl_FragColor = vec4(0.0, 0.0, 0.0, alpha);" +
"}")
gradientShader.uniforms = [SKUniform(name: "npcLocation", vectorFloat2: vector_float2(Float(x), Float(y)))]
n.sight.fillColor = .clear
n.sight.fillShader = gradientShader

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.

CAShapeLayer background-color fading to sides in Swift

I have created a clickable diagram with CAShapeLayers and UIBezierPaths. These shapes have a background-color that I want to fade out to transparent on the sides/edges. All CAShapeLayers have different shapes. Is there a way to achieve this in Swift?
I attached a simple example image of what I'm trying to do:
Right now, I'm creating the CAShapeLayers as follows:
var path = UIBezierPath()
path.moveToPoint(CGPointMake(20, 30))
path.addLineToPoint(CGPointMake(40, 30))
path.addLineToPoint(CGPointMake(50, 60))
path.addLineToPoint(CGPointMake(120, 180))
path.closePath()
var layer = CAShapeLayer()
layer.path = path.CGPath
layer.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.5).CGColor
layer.hidden = true
baseImg.layer.addSublayer(layer)
Is there a way to add a CIGaussianBlur or a UIBlurEffect to these shapes?