如何启动函数drawRect(rect:CGRect)?迅速

时间:2016-10-11 18:00:29

标签: ios swift cgrect

我的应用内有rect:CGRect功能,我该如何使用它?我试试

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

但是我的功能不在下面。

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


}

2 个答案:

答案 0 :(得分:1)

您永远不会自己致电drawRect。它在需要时由框架调用。

正确的解决方案是在视图上调用setNeedsDisplay()。只要视图状态发生变化,您就希望调用drawRect,请调用此方法。

除此之外,您使用了错误的drawRect方法。 UIView中的一个只有一个参数 - CGRect

在Swift 3中它将是:

override func draw(_ rect: CGRect) {
}

答案 1 :(得分:1)

你不应该调用drawRect()。如果要查看重绘,请调用setNeedsDisplay()方法。

相关问题