Swift and SpriteKit: how to implement non-fuzzy circle timer with SKShapeNode - ios

The code below implements a circle timer with SKShapeNode where one slice of the circle vanishes as time ticks off.
Unfortunately, the timer is fuzzy because of limitations with SKShapeNode.
Answers like this did not address the issue, and also were old and may no longer apply.
1) Is there a way to use SKShapeNode without such fuzzy lines?
2) If there is no way to fix SKShapdeNode, what are the alternatives for implementing a similar circle timer?
let timer = SKShapeNode(circleOfRadius: CGFloat(50))
timer.fillColor = UIColor.red
timer.strokeColor = UIColor.clear
timer.lineWidth = 0
timer.zRotation = CGFloat.pi / 2
timer.path = self.circle(radius: CGFloat(50), percent: CGFloat(90))
fileprivate func circle(radius: CGFloat, percent: CGFloat) -> CGPath {
// Adjust <percent> if less than 0
let percent = percent < 0 ? 0 : percent
// Generate circle path
let start = CGFloat(0)
let end = CGFloat.pi * 2 * percent
let center = CGPoint.zero
let bezierPath = UIBezierPath()
bezierPath.move(to: center)
bezierPath.addArc(withCenter:center, radius: radius, startAngle: start, endAngle: end, clockwise: true)
bezierPath.addLine(to: center)
// Return path
return bezierPath.cgPath
}

Shaders will probably accomplish what you want, check out http://battleofbrothers.com/sirryan/understanding-shaders-in-spritekit/
Also, SKShapeNode should not be used outside of debugging according to AppleDocs, have ran into a few issues using them in the past.

#ElTomato mentioned setting a stroke color and line width, this will work in masking the fuzziness.
A better solution may be to just turn off anti-aliasing with isAntialiased = false
This would depend on your graphic preference of course.

Related

Smooth open UIBezierPath

I'm trying to get smooth edges of Arcs and Curves.
The SKShapeNodes are open UIBezierPaths with a thick line-width, however, jagged edges are noticeable around Arcs and Curves. Changing the flatness doesn't seem to have any effect. Is there a way to smoothen these, or am I going to have to make a closed UIBezierPath with no line-width?
You need to create closed UIBezierPath with no linewidth
also
Make it shouldRasterize = true and set proper scale rasterizationScale = 2 * UIScreen.main.scale
I have a solution to my problem. Instead of using path.addArc(), I now use a function to get as many points as I'd like along an arc and then use path.addLine() to every one of those points. If you want more points for a smoother path, just lower the value of n.
func getCirclePoints(centerPoint: CGPoint, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Bool) -> [CGPoint] {
let n : CGFloat = clockwise ? -1 : 1
let points: [CGPoint] = stride(from: startAngle - n, through: endAngle + n, by: n).map {
let degreesToRadians = CGFloat($0) * .pi / 180
let x = centerPoint.x + radius * cos(degreesToRadians)
let y = centerPoint.y + radius * sin(degreesToRadians)
return CGPoint(x: x, y: y)
}
return points
}

How I can create this circular shape in iOS Swift4?

I have to create this shape like the below image in a UIView. But I am not get any idea how to draw this shape. I am trying to draw this shape using UIBezierPath path with CAShapeLayer the circular line draw successfully but I am unable to draw the circular points and circular fill colour. Can anyone please suggest me how I can achieve this type shape using any library or UIBezierPath.
This is my code which I am using try to draw this circular shape.
class ViewController: UIViewController {
var firstButton = UIButton()
var mylabel = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
self.creatingLayerWithInformation()
}
func creatingLayerWithInformation(){
let safeAreaHeight = self.view.safeAreaInsets.top
let navBarHeight = self.navigationController?.navigationBar.frame.height
self.addLayer(isClockWise: true, radius: self.view.frame.width * 0.72, xPoint: 0, yPoint: navBarHeight!, layerColor: UIColor.green, fillcolor: .clear)
self.addLayer(isClockWise: true, radius: self.view.frame.width * 0.72, xPoint: self.view.frame.width, yPoint: self.view.frame.height - 150, layerColor: UIColor.blue, fillcolor: .clear)
let aa = self.view.frame.width * 0.72
self.addLayer(isClockWise: true, radius: 10, xPoint: 0+aa, yPoint: navBarHeight!+5, layerColor: UIColor.blue, fillcolor: .clear)
self.addLayer(isClockWise: true, radius: 10, xPoint: 0+15, yPoint: navBarHeight!+aa, layerColor: UIColor.blue, fillcolor: .clear)
}
func addLayer(isClockWise: Bool, radius: CGFloat, xPoint: CGFloat, yPoint: CGFloat, layerColor: UIColor, fillcolor: UIColor) {
let pi = CGFloat(Float.pi)
let start:CGFloat = 0.0
let end :CGFloat = 20
// circlecurve
let path: UIBezierPath = UIBezierPath();
path.addArc(
withCenter: CGPoint(x:xPoint, y:yPoint),
radius: (radius),
startAngle: start,
endAngle: end,
clockwise: isClockWise
)
let layer = CAShapeLayer()
layer.lineWidth = 3
layer.fillColor = fillcolor.cgColor
layer.strokeColor = layerColor.cgColor
layer.path = path.cgPath
self.view.layer.addSublayer(layer)
}}
But I am getting the below result.
Please suggest me how I can achieve this shape. Correct me if I am doing anything wrong. If there is any library present then also please suggest. Please give me some solution.
Advance thanks to everyone.
There are many ways to achieve this effect, but a simple solution is to not draw the large circle as a single arc, but rather as a series of arcs that start and stop at the edges of the smaller circles. To do this, you need to know what the offset is from the inner circles. Doing a little trigonometry, you can calculate that as:
let angleOffset = asin(innerRadius / 2 / mainRadius) * 2
Thus:
let path = UIBezierPath()
path.move(to: point(from: arcCenter, radius: mainRadius, angle: startAngle))
let anglePerChoice = (endAngle - startAngle) / CGFloat(choices.count)
let angleOffset = asin(innerRadius / 2 / mainRadius) * 2
var from = startAngle
for index in 0 ..< choices.count {
var to = from + anglePerChoice / 2 - angleOffset
path.addArc(withCenter: arcCenter, radius: mainRadius, startAngle: from, endAngle: to, clockwise: true)
to = from + anglePerChoice
from += anglePerChoice / 2 + angleOffset
path.move(to: point(from: arcCenter, radius: mainRadius, angle: from))
path.addArc(withCenter: arcCenter, radius: mainRadius, startAngle: from, endAngle: to, clockwise: true)
from = to
}
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = strokeColor.cgColor
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.lineWidth = lineWidth
layer.addSublayer(shapeLayer)
Where:
func point(from point: CGPoint, radius: CGFloat, angle: CGFloat) -> CGPoint {
return CGPoint(x: point.x + radius * cos(angle),
y: point.y + radius * sin(angle))
}
That yields:
So, when you then add the inner circles:
While the above is simple, it has limitations. Specifically if the lineWidth of the big arc was really wide in comparison to that of the small circles, the breaks in the separate large arcs won’t line up nicely with the edges of the small circles. E.g. imagine that the small circles had a radius 22 points, but that the big arc’s stroke was comparatively wide, e.g. 36 points.
If you have this scenario (not in your case, but for the sake of future readers), the other approach, as matt suggested, is to draw the big arc as a single stroke, but then mask it using the paths for the small circles.
So, imagine that you had:
a single CAShapeLayer, called mainArcLayer, for the big arc; and
an array of UIBezierPath, called smallCirclePaths, for all of the small circles.
You could then create a mask in layoutSubviews of your UIView subclass (or viewDidLayoutSubviews in your UIViewController subclass) like so:
override func layoutSubviews() {
super.layoutSubviews()
let path = UIBezierPath(rect: bounds)
smallCirclePaths.forEach { path.append($0) }
let mask = CAShapeLayer()
mask.fillColor = UIColor.white.cgColor
mask.strokeColor = UIColor.clear.cgColor
mask.lineWidth = 0
mask.path = path.cgPath
mask.fillRule = .evenOdd
mainArcLayer.mask = mask
}
That yields:
This is a slightly more generalized solution to this problem.
Think about the problem in the simplest possible form. Imagine a straight line with one small circle superimposed over the middle of it.
Let's divide our thinking into three layers:
The background that needs to show through
The layer that holds the straight line
The layer that holds the small circle
Calculate the location of the small circle layer relative to the straight line layer.
Place the small circle layer at that location. Okay, but the straight line shows through.
Go back to the straight line layer. Give it a mask. Construct that mask with a transparent circle at exactly the location of the small circle layer.
Now the mask "punches a hole" through the straight line — at exactly the place where the circle covers it. Thus we appear to see through the circle to the background, because the straight line is missing at exactly that place.
In real life there will be multiple circle layers and the mask will have multiple transparent circles and the straight line will be a curve, but that is a minor issue once you have the hole-punching worked out.

Circular loading bar with image

I want to create an achievement system in my game. I want players to see their % achievement progress like this.
For example, if he completed 65% -> 75% -> 100% to look like this.
https://imgur.com/a/P3l6hOQ
Is there any way to crop the image circular to look like a circular progess bar? A direction, a hint, a pod or a piece of code is appreciated.
My images will be rectangles with rounded corners and the code is written in SpriteKit.
Managed to solve it. Indeed the best and easiest solution was to create a mask_node.
let button_image : SKSpriteNode = SKSpriteNode()
let crop_node : SKCropNode = SKCropNode()
let completion : CGFloat = 0.83 // from 0.0 to 1.0
let circle_bezier_path = UIBezierPath(arcCenter: CGPoint.zero, radius: 200, startAngle: 0, endAngle: CGFloat.pi * 2 * completion, clockwise: true)
circle_bezier_path.addLine(to: CGPoint(x: 0, y: 0))
let mask_node : SKShapeNode = SKShapeNode(path: circle_bezier_path.cgPath)
mask_node.zRotation = CGFloat.pi/2
let wrapper_node = SKNode()
wrapper_node.addChild(mask_node)
crop_node.maskNode = wrapper_node
crop_node.addChild(button_image)
addChild(crop_node)
Good luck!

Swift draw several paths in loops

I'm working on changing my previously circlechart, to a donutchart (That can support more than 1 number).
I got all the angle's correct, changed my data to work with an array.
But when I try to draw my paths, it only draws the last one.
Here's an example screenshot of the issue:
In the above example, it's set to draw this array:
cell.circleChart.percentFills = [weight, 10]
where weight is the % shown in the label of the circle. So you can see, that it doesn't draw the first angle, but the starting point for the next angle is correct.
Here's the code where I draw my donut chart.
override func drawRect(rect: CGRect) {
// Define the center.
let center = CGPoint(x:bounds.width/2, y: bounds.height/2)
// Define the radius
let radius: CGFloat = max(bounds.width, bounds.height)
// Define starting and ending angles
var startAngle: CGFloat = CGFloat(DegreesToRadians(-90))
let endAngle: CGFloat = CGFloat(DegreesToRadians(270))
// Set up the full path. (for the default background color)
var path = UIBezierPath(arcCenter: center,
radius: bounds.width/2 - arcWidth/2,
startAngle: startAngle,
endAngle: endAngle,
clockwise: true)
// draw the full chart with the background color
path.lineWidth = arcWidth
chartColor.setStroke()
path.stroke()
//calculate the arc for each per percent
let arcLengthPerPercent = CGFloat(DegreesToRadians(360/100))
// Temporary var for loop testing
var i = 0
println("startAngle before loop: \(RadiansToDegrees(Double(startAngle)))")
for percentFill in percentFills {
println("LOOP: \(i)")
if i == 0 {
fillColor = UIColor.formulaBlueColor()
println("BLUE")
} else {
fillColor = UIColor.formulaGreenColor()
println("GREEN")
}
//then multiply out by the actual percent
let fillEndAngle = arcLengthPerPercent * CGFloat(percentFill) + startAngle
println("fillEndAngle: \(RadiansToDegrees(Double(fillEndAngle)))")
//2 - draw the outer arc
var fillPath = UIBezierPath(arcCenter: center,
radius: bounds.width/2 - arcWidth/2,
startAngle: startAngle,
endAngle: fillEndAngle,
clockwise: true)
progressLine.path = fillPath.CGPath
progressLine.strokeColor = fillColor.CGColor
progressLine.fillColor = UIColor.clearColor().CGColor
progressLine.lineWidth = arcWidth
// add the curve to the screen
self.layer.addSublayer(progressLine)
i++
startAngle = startAngle+(arcLengthPerPercent * CGFloat(percentFill))
}
}
I reckon I'm probably going wrong in the last bit, where I add the progressLine as a subLayer, but I don't know what else I should be doing here instead.
Of course I've tested that if I only have 1 value in the array, it draws that as intended (in the blue color)
Any help getting several paths drawn out, would be greatly appreciated!
But when I try to draw my paths, it only draws the last one.
The problem is that progressLine is declared outside this routine. Therefore you are just adding the same path layer over and over as a sublayer - not multiple path sublayers. Therefore it appears only once in your interface, containing the path (segment) that you most recently assigned to it — the last one in the last iteration of the loop.

animating circular UIBezierPath without rounding the stroke

I'm trying to add an animation to my UIBezierPath, but I want to keep the hard edges my current fillPath is creating, here's a screenshot of what the result should look like:
I did manage to get one animation working correctly using a slightly modified version of this code:
// create an object that represents how the curve
// should be presented on the screen
let progressLine = CAShapeLayer()
progressLine.path = ovalPath.CGPath
progressLine.strokeColor = UIColor.blueColor().CGColor
progressLine.fillColor = UIColor.clearColor().CGColor
progressLine.lineWidth = 10.0
progressLine.lineCap = kCALineCapRound
// add the curve to the screen
self.view.layer.addSublayer(progressLine)
// create a basic animation that animates the value 'strokeEnd'
// from 0.0 to 1.0 over 3.0 seconds
let animateStrokeEnd = CABasicAnimation(keyPath: "strokeEnd")
animateStrokeEnd.duration = 3.0
animateStrokeEnd.fromValue = 0.0
animateStrokeEnd.toValue = 1.0
// add the animation
progressLine.addAnimation(animateStrokeEnd, forKey: "animate stroke end animation")
The above code is from: http://mathewsanders.com/animations-in-swift-part-two/
It did work, but the stroke it created was rounded.
The only examples/tutorials and other questions I could find on this matter, was using CAShapes. I'm currently not using CAShapes or layers. (I don't know if that's needed to make an animation work with this, but I figure I'd ask)
Here's my current code (without any animation). To draw my little circle chart.
class CircleChart: UIView {
var percentFill: Float = 99.00 {
didSet {
// Double check that it's less or equal to 100 %
if percentFill <= maxPercent {
//the view needs to be refreshed
setNeedsDisplay()
}
}
}
var fillColor: UIColor = UIColor.formulaBlueColor()
var chartColor: UIColor = UIColor.formulaLightGrayColor()
override func drawRect(rect: CGRect) {
// Define the center.
let center = CGPoint(x:bounds.width/2, y: bounds.height/2)
// Define the radius
let radius: CGFloat = max(bounds.width, bounds.height)
// Define the width of the circle
let arcWidth: CGFloat = 10
// Define starting and ending angles (currently unused)
let startAngle: CGFloat = degreesToRadians(-90)
let endAngle: CGFloat = degreesToRadians(270)
// 5
var path = UIBezierPath(arcCenter: center,
radius: bounds.width/2 - arcWidth/2,
startAngle: startAngle,
endAngle: endAngle,
clockwise: true)
// 6
path.lineWidth = arcWidth
chartColor.setStroke()
path.stroke()
//Draw the fill-part
//calculate the arc for each per percent
let arcLengthPerPercent = degreesToRadians(360/100)
//then multiply out by the actual percent
let fillEndAngle = arcLengthPerPercent * CGFloat(percentFill) + startAngle
//2 - draw the outer arc
var fillPath = UIBezierPath(arcCenter: center,
radius: bounds.width/2 - arcWidth/2,
startAngle: startAngle,
endAngle: fillEndAngle,
clockwise: true)
//3 - draw the inner arc
fillPath.addArcWithCenter(center,
radius: bounds.width/2 - arcWidth/2,
startAngle: fillEndAngle,
endAngle: startAngle,
clockwise: false)
//4 - close the path
fillPath.closePath()
fillColor.setStroke()
fillPath.lineWidth = arcWidth
fillPath.stroke()
}
}
What would be the best way to animate the fillPath? Or do I need to redo everything in CAShapes and layers, to make it work?
Any help would be greatly appreciated.
If you want it square, why are you using progressLine.lineCap = kCALineCapRound? Just delete that line, and you'll get the default butt end (kCALineCapButt).

Resources