-2

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()


}
SwiftDeveloper
  • 7,244
  • 14
  • 56
  • 85
  • 1
    better to call `setNeedsDisplay` which can call `drawRect` itself for your view. Ultimately `drawRect` is called by itself whenever a view of its type is initialized – Rajan Maheshwari Oct 11 '16 at 18:04
  • 2
    What doesn't work? That's **not** the `drawRect` method called by `setNeedsDisplay` or similar. – vadian Oct 11 '16 at 18:04
  • 1
    What is this custom `drawRect` method you have? It's not the right one from `UIView`. – rmaddy Oct 11 '16 at 18:05
  • 1
    ohh yes.. That is something different and not the one from `UIView` – Rajan Maheshwari Oct 11 '16 at 18:10
  • You better rename your methode to get better answers and not frighten the friendly iOS reader. I supect you want to draw somthing which you can see on screen. In this case this answer might help: http://stackoverflow.com/a/21312119/1457385 – shallowThought Oct 11 '16 at 18:22

2 Answers2

2

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) {
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
1

You should not call drawRect(). When you want to view to redraw, call the setNeedsDisplay() method.

TheAppMentor
  • 1,091
  • 7
  • 14