简体   繁体   中英

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


}

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) {
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM