在Swift中调用类来显示drawRect

时间:2014-09-29 22:10:50

标签: ios class swift

基本上,我有一个包含drawRect函数的类,我用它来显示循环进度条。

我的问题是我不确定如何显示进度圈?我之前从未参与过课程,所以我不知道我是如何与班级互动的。

这是我的代码:

class ProgressCircle: UIView {

override func drawRect(rect: CGRect) {
    var ctx = UIGraphicsGetCurrentContext()

    var progress: CGFloat = 0.7
    var innerRadiusRatio: CGFloat = 0.5

    var path: CGMutablePathRef = CGPathCreateMutable()
    var startAngle: CGFloat = CGFloat(-M_PI_2)
    var endAngle: CGFloat = CGFloat(-M_PI_2) + min(1.0, progress) * CGFloat(M_PI * 2)
    var outerRadius: CGFloat = CGRectGetWidth(self.bounds) * 0.5 - 1.0
    var innerRadius: CGFloat = outerRadius * innerRadiusRatio
    var center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect))

    CGPathAddArc(path, nil, center.x, center.y, innerRadius, startAngle, endAngle, false)
    CGPathAddArc(path, nil, center.x, center.y, outerRadius, endAngle, startAngle, true)
    CGPathCloseSubpath(path)
    CGContextAddPath(ctx, path)

    CGContextSaveGState(ctx)
    CGContextClip(ctx)
    CGContextDrawImage(ctx, self.bounds, UIImage(named: "RadialProgressFill").CGImage)
    CGContextRestoreGState(ctx)
}

}

我在SecondViewController类上面有这个代码,我打算在第二个视图控制器中的一个视图(' budgetDisplayView')中显示循环进度条。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您应该将您的ProgressCircle添加为subView。

class SecondViewController: UIViewController {

//or add where you want or an event
 override func viewDidAppear(animated: Bool) {
   //give the appropriate frame
    var circle = ProgressCircle(frame: self.view.bounds)
    self.budgetDisplayView.addSubview(circle)
 }
}