Create a line graph with gradients in iOS - ios

What's the best way to create a graph like this in iOS?
My first thought was to create a bezier path and then add a gradient layer with the different locations. But this can't work since the documentation specifies that:
The values must be monotonically increasing.
Which is not the case in my graph.
Any thoughts on good ways to achieve this?
Thanks

You can do this by using a CAGradientLayer as the background of your chart, and then a CAShapeLayer as a mask of the gradient layer. The mask layer will only show the layer beneath in areas that it is drawn on.
This playground code gives a general idea, using randomly generated data:
import UIKit
import PlaygroundSupport
let view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
view.backgroundColor = .black
// Gradient for the chart colors
let gradient = CAGradientLayer()
gradient.colors = [
UIColor.red.cgColor,
UIColor.orange.cgColor,
UIColor.yellow.cgColor,
UIColor.green.cgColor
]
gradient.startPoint = CGPoint(x: 0.5, y: 0)
gradient.endPoint = CGPoint(x: 0.5, y: 1)
gradient.frame = view.bounds
view.layer.addSublayer(gradient)
// Random points
let graph = CAShapeLayer()
let path = CGMutablePath()
var y: CGFloat = 150
let points: [CGPoint] = stride(from: CGFloat.zero, to: 300, by: 2).map {
let change = CGFloat.random(in: -20...20)
var newY = y + change
newY = max(10, newY)
newY = min(newY, 300)
y = newY
return CGPoint(x: $0, y: y)
}
path.addLines(between: points)
graph.path = path
graph.fillColor = nil
graph.strokeColor = UIColor.black.cgColor
graph.lineWidth = 4
graph.lineJoin = .round
graph.frame = view.bounds
// Only show the gradient where the line is
gradient.mask = graph
PlaygroundPage.current.liveView = view
Results:

Related

iOS Circular Gradient

I have a task to draw the line with a circular gradient (colour should change by the circle) and then add animation. Now I draw 360 layers with a certain interval and different colours.
var colours: [UIColor] = [UIColor]()
var startAngle = CGFloat(-0.5 * Double.pi)
var index = 0
func drawLayers() {
let smallAngle = (1.5 * CGFloat.pi - (-0.5 * CGFloat.pi)) / 360
if index < colours.count { //colours.count = 360
let endAngle = startAngle + smallAngle
let circlePath = UIBezierPath(arcCenter: .init(x: 100, y: 100), radius: 100, startAngle: startAngle, endAngle: endAngle, clockwise: true)
startAngle = endAngle
let shapeLayer = CAShapeLayer()
shapeLayer.path = circlePath.cgPath
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = colours[index].cgColor
shapeLayer.lineWidth = 8
view.layer.addSublayer(shapeLayer)
index += 1
Timer.scheduledTimer(
withTimeInterval: 0.004,
repeats: false) { (_) in
self.drawLayers()
}
}
}
Something like that but with linear animation
Can anyone tell me how to do it right?
iOS has circular (conic) gradients built in now. So I would just ask for the gradient, once, and then animate a single path used as a mask. That’s just two layers, much less work, true animation, and a true gradient.
Example:
Here's my test code; change the colors and numbers as desired:
let grad = CAGradientLayer()
grad.type = .conic
grad.colors = [UIColor.red.cgColor, UIColor.green.cgColor, UIColor.red.cgColor]
grad.startPoint = CGPoint(x: 0.5, y: 0.5)
grad.frame = CGRect(x: 100, y: 100, width: 200, height: 200)
self.view.layer.addSublayer(grad)
let c = CAShapeLayer()
let p = UIBezierPath(ovalIn: CGRect(x: 20, y: 20, width: 160, height: 160))
c.path = p.cgPath
c.fillColor = UIColor.clear.cgColor
c.strokeColor = UIColor.black.cgColor
c.lineWidth = 8
grad.mask = c
c.strokeEnd = 0
To make the animation happen, just say:
c.strokeEnd = 1

Adding the same layer in a subview changes its visual position

I added a subview (with a black border) in a view and centered it.
Then I generate 2 identical triangles with CAShapeLayer and add one to the subview and the other to the main view.
Here is the visual result in Playground where we can see that the green triangle is totally off and should have been centered.
And here is the code:
let view = UIView()
let borderedView = UIView()
var containedFrame = CGRect(x: 0, y: 0, width: 100, height: 100)
func setupUI() {
view.frame = CGRect(x: 0, y: 0, width: 300, height: 600)
view.backgroundColor = .white
borderedView.frame = containedFrame
borderedView.center = view.center
borderedView.backgroundColor = .clear
borderedView.layer.borderColor = UIColor.black.cgColor
borderedView.layer.borderWidth = 1
view.addSubview(borderedView)
setupTriangles()
}
private func setupTriangles() {
view.layer.addSublayer(createTriangle(color: .red)) // RED triangle
borderedView.layer.addSublayer(createTriangle(color: .green)) // GREEN triangle
}
private func createTriangle(color: UIColor) -> CAShapeLayer {
let layer = CAShapeLayer()
let bezierPath = UIBezierPath()
bezierPath.move(to: CGPoint(x: 0, y: 0))
bezierPath.addLine(to: CGPoint(x: -containedFrame.width, y: 0))
bezierPath.addLine(to: CGPoint(x: 0, y: -containedFrame.height))
bezierPath.close()
layer.position = borderedView.center
layer.path = bezierPath.cgPath
layer.fillColor = color.cgColor
return layer
}
Note: All position (of view, the borderedView and both triangles) are the same (150.0, 300.0)
Question: Why is the green layer not in the right position?
#DuncanC is right that each view has its own coordinate system. Your problem is this line:
layer.position = borderedView.center
That sets the layer's position to the center of the frame for the borderedView which is in the coordinate system of view. When you create the green triangle, it needs to use the coordinate system of borderedView.
You can fix this by passing the view to your createTriangle function, and then use the center of the bounds of that view as the layer position:
private func setupTriangles() {
view.layer.addSublayer(createTriangle(color: .red, for: view)) // RED triangle
borderedView.layer.addSublayer(createTriangle(color: .green, for: borderedView)) // GREEN triangle
}
private func createTriangle(color: UIColor, for view: UIView) -> CAShapeLayer {
let layer = CAShapeLayer()
let bezierPath = UIBezierPath()
bezierPath.move(to: CGPoint(x: 0, y: 0))
bezierPath.addLine(to: CGPoint(x: -containedFrame.width, y: 0))
bezierPath.addLine(to: CGPoint(x: 0, y: -containedFrame.height))
bezierPath.close()
layer.position = CGPoint(x: view.bounds.midX, y: view.bounds.midY)
layer.path = bezierPath.cgPath
layer.fillColor = color.cgColor
return layer
}
Note: When you do this, the green triangle appears directly below the red one, so it isn't visible.
Every view/layer uses the coordinate system of it's superview/superlayer. If you add a layer to self.view.layer, it will be positioned in self.view.layer's coordinate system. If you add a layer to borderedView.layer, it will be in borderedView.layer's coordinate system.
Think of the view/layer hierarchy as stacks of pieces of graph paper. You place a new piece of paper on the current piece (the superview/layer) in the current piece's coordinates system, but then if you draw on the new view/layer, or add new views/layer inside that one, you use the new view/layer's coordinate system.

CAGradientLayer is not getting the shape

I am creating a CAGradientLayer with the shape of a horseshoe. It seems it's not taking the path and it just draws a square with shadow. What am I missing ?
let gradientLayerContainer = CAShapeLayer()
let gradient = CAGradientLayer()
let segmentGradientPath = UIBezierPath.horseshoe(
center: centerPoint,
innerRadius: (bounds.width / 2) - config.ringWidth,
outerRadius: bounds.width / 2,
startAngle: segmentTapped.startAngle,
endAngle: segmentTapped.endAngle)
gradientLayerContainer.fillColor = UIColor.clear.cgColor
gradientLayerContainer.path = segmentGradientPath.cgPath
let end = centerPoint
let start = centerPoint.shifted(outerRadius, with: segmentTapped.centerAngle)
gradient.startPoint = CGPoint(x: start.x / bounds.width, y: start.y / bounds.height)
gradient.endPoint = CGPoint(x: end.x / bounds.width, y: end.y / bounds.height)
gradient.colors = [UIColor.white.withAlphaComponent(0.0).cgColor, UIColor.white.cgColor]
gradient.frame = segmentGradientPath.bounds
gradient.mask = gradientLayerContainer
gradient.locations = [0, 1]
horseshoeLayer.addSublayer(gradientLayerContainer)
gradientLayerContainer.insertSublayer(gradient, at: 0)
The mask belongs to the gradient layer. The gradient layer does not belong to the mask. Also the mask color needs to be opaque everywhere you want the gradient to show (you are using clear which is the opposite).
Here is a Playground example of the correct hierarchy:
import PlaygroundSupport
import UIKit
let rect = CGRect(x: 0, y: 0, width: 300, height: 300)
let gradient = CAGradientLayer()
let maskLayer = CAShapeLayer()
let maskPath = UIBezierPath(ovalIn: rect)
maskLayer.fillColor = UIColor.black.cgColor
maskLayer.path = maskPath.cgPath
gradient.colors = [UIColor.blue.cgColor, UIColor.red.cgColor]
gradient.mask = maskLayer
gradient.locations = [0, 1]
let view = UIView(frame: rect)
view.layer.addSublayer(gradient)
gradient.frame = view.bounds
PlaygroundPage.current.liveView = view

Subclassed MaterialView not showing MaterialDepth

I have subclassed MaterialView and override drawRect to make a custom view which I then add as my tableViewFooter. As the title says I cannot get the depth to work. I've messed around with clipsToBounds and masksToBounds on varying layers/views without success. It is a tableView inside of a UIViewController just for clarity of my code.
let receiptEdge = ReceiptEdge(frame: CGRect(x: 0, y: 0, width: self.view.w, height: 30))
receiptEdge.backgroundColor = .whiteColor()
receiptEdge.depth = MaterialDepth.Depth5
//What I've tried messing with for an hour
receiptEdge.clipsToBounds = false
receiptEdge.layer.masksToBounds = true
self.tableView.clipsToBounds = false
self.view.clipsToBounds = false
self.view.layer.masksToBounds = true
self.tableView.layer.masksToBounds = true
self.tableView.tableFooterView = receiptEdge
My code for subclass ReceiptEdge
override func prepareView() {
super.prepareView()
let rect = self.frame
let path = UIBezierPath()
let receiptEdgeSize = CGFloat(rect.width / 75)
var x = CGFloat(-receiptEdgeSize / 2)
let y = rect.size.height / 2
path.moveToPoint(CGPoint(x: x, y: y))
while x < rect.width {
x += receiptEdgeSize
path.addLineToPoint(CGPoint(x: x, y: y))
x += receiptEdgeSize
path.addArcWithCenter(CGPoint(x: x, y: y), radius: receiptEdgeSize, startAngle: CGFloat(M_PI), endAngle: CGFloat(0), clockwise: true)
x += receiptEdgeSize
}
path.addLineToPoint(CGPoint(x: x, y: CGFloat(0)))
path.addLineToPoint(CGPoint(x: 0, y: 0))
path.addLineToPoint(CGPoint(x: 0, y: y))
let layer = CAShapeLayer()
layer.path = path.CGPath
self.layer.mask = layer
self.visualLayer.mask = layer
self.visualLayer.backgroundColor = UIColor.blueColor().CGColor
self.layer.shadowColor = UIColor.redColor().CGColor
}
Screenshot showing no depth
Thanks in advance for someone that knows about layers more than I!
Here is a an example of a view where I have gotten depth to work on a subclass of MaterialTableViewCell using MaterialDepth.Depth2
Here is with layer having same path of visualLayer
Here I set no layer or path to self.layer. There is shadow but view or shadow not clipping to visual layer. set shadowColor to red so you could see the difference.
So the way depth works.
MaterialView is a composite object, which uses two layers to achieve its ability to clip the bounds and still offer a shadowing. A CAShapLayer called visualLayer is added as a sublayer to the backing layer of a view. So when an image is added, it is actually added to the visualLayer, which has the masksToBounds property set to true, and the layer property has its masksToBounds property set to false. This allows the depth property to be displayed on the layer property and still give the perception of a clipping.
In this line
receiptEdge.layer.masksToBounds = true
You are actually clipping the depth. So what I would try and do, is set this
let layer = CAShapeLayer()
layer.path = path.CGPath
self.layer.mask = layer
to the visualLayer property.
That should help you out, if not get you on the right path.

How to give "locations" to CAGradientLayer

I have created CAGradientLayer having three colors, i need to give each color different locations.
Example:
Red = 0 to 50 %
Yellow = 51 to 80 %
Green = 81 to 100 %
I have tried with giving startPoint and endPoint but it did not work.
If you put the following code into a Playground you will get the exact desired output:
let view = UIView(frame: CGRectMake(0,0,200,100))
let layer = CAGradientLayer()
layer.frame = view.frame
layer.colors = [UIColor.greenColor().CGColor, UIColor.yellowColor().CGColor, UIColor.redColor().CGColor]
layer.locations = [0.0, 0.8, 1.0]
view.layer.addSublayer(layer)
XCPShowView("ident", view: view)
Outputting:
You simply define the colors as an array of CGColors, and an array of the same size of NSNumbers each between 0.0 and 1.0.
Dont use startPoint and endPoint for that - they are for defining from where to where the gradient is shown in the layer - it does not have anything to do with the percents and the colors etc.
More recent Swift3 version of the code:
let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
let layer = CAGradientLayer()
layer.frame = view.frame
layer.colors = [UIColor.green.cgColor, UIColor.yellow.cgColor, UIColor.red.cgColor]
layer.locations = [0.0, 0.8, 1.0]
view.layer.addSublayer(layer)
PlaygroundPage.current.liveView = view

Resources