SwiftDeveloper SwiftDeveloper - 1 year ago 166
Swift Question

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

I have

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


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


Answer Source

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) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download