SwiftDeveloper SwiftDeveloper - 1 month ago 16
Swift Question

How to start function drawRect(rect: CGRect) ? Swift

I have

rect:CGRect
function inside my app, how can i use it ? I try

drawRect(CGRect,x1:33.33,y1:23.45)


but doesn't work my function under below.

func drawRect(rect: CGRect,x1: CGFloat, y1: CGFloat) {

let center = CGPoint(x:x1/2, y: y1/2)
let radius: CGFloat = max(bounds.width, bounds.height)
let arcWidth: CGFloat = 100
let startAngle: CGFloat = 3 * π / 5.99
let endAngle: CGFloat = π / 1.40

let path = UIBezierPath(arcCenter: center,
radius: radius/2 - arcWidth/2,
startAngle: startAngle,
endAngle: endAngle,
clockwise: true)


path.lineWidth = arcWidth
counterColor.setStroke()
path.stroke()


}

Answer

You never call drawRect yourself. It's called by the framework when needed.

The proper solution is to call setNeedsDisplay() on the view. Call this whenever the state of the view changes such that you want drawRect to be called.

On top of this, you have the wrong drawRect method. The one from UIView only has one parameter - the CGRect.

And in Swift 3 it would be:

override func draw(_ rect: CGRect) {
}
Comments