Closed pankti28 closed 4 months ago
Solution:
strokeColor.setStroke()
path.stroke()
These two lines caused the error.
Apparently, You can't do any of those things, in Objective-C or in Swift, unless you are in a graphics context (e.g. drawRect: or UIGraphicsBeginImageContext).
so instead use CAShapeLayer
func createArcWith(startAngle: CGFloat, endAngle: CGFloat, arcCap: CGLineCap, strokeColor: UIColor, center:CGPoint) {
// 1
let center = center
let radius: CGFloat = max(bounds.width, bounds.height)/2 - self.frame.width/20
let lineWidth: CGFloat = self.frame.width/10
// 2
let path = UIBezierPath(arcCenter: center,
radius: radius,
startAngle: startAngle,
endAngle: endAngle,
clockwise: true)
// 3
let arcLayer = CAShapeLayer()
arcLayer.path = path.cgPath
arcLayer.strokeColor = strokeColor.cgColor
arcLayer.fillColor = UIColor.clear.cgColor
arcLayer.lineWidth = lineWidth
layer.addSublayer(arcLayer)
}
seems like this function
throws the error for invalid context 0x0