Luke Brandon Farrell
Luke Brandon Farrell

Reputation: 511

Circular Progress Bar help, Swift Programming Language

I'm trying to make a small circular progress bar in swift,

This is what I have so far

 private var Timer:CGFloat = 100;

 override func update(currentTime: CFTimeInterval) 
 {
    /* Called before each frame is rendered */
    var StartAngle:CGFloat!
    var EndAngle:CGFloat!

    var circleTimer:UIBezierPath!

    StartAngle = CGFloat(M_PI * 1.5)
    EndAngle = StartAngle + CGFloat((M_PI * 2))

    var Progress:CGFloat = (EndAngle - StartAngle) * (Timer / 100) + StartAngle

    circleTimer.addArcWithCenter(CGPointMake(200, 200), radius: CGFloat(130), startAngle: StartAngle, endAngle: Progress, clockwise: true)

    Timer--;
}

I get this error when I run the App

"BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)"

on this line

"circleTimer.addArcWithCenter(CGPointMake(200, 200), radius: CGFloat(130), startAngle: StartAngle, endAngle: Progress, clockwise: true)"

I'm fairly new to Swift (Programmed in other languages) and I can't seem to work it out.

Thanks Luke

Upvotes: 0

Views: 3979

Answers (1)

codester
codester

Reputation: 37189

You have not initialized circleTimer so circleTimer is nil.You should initialize it first than call

circleTimer.addArcWithCenter(CGPointMake(200, 200), radius: CGFloat(130), startAngle: StartAngle, endAngle: Progress, clockwise: true)

Intialize it with path

var circleTimer:UIBezierPath! = UIBezierPath()

Also write this in draw rect

 override func drawRect(rect: CGRect) {

    /* Called before each frame is rendered */
    var StartAngle:CGFloat!
    var EndAngle:CGFloat!

    var circleTimer:UIBezierPath! = UIBezierPath()
    StartAngle = CGFloat(M_PI * 1.5)
    EndAngle = StartAngle + CGFloat((M_PI * 2))

    var Progress:CGFloat = (EndAngle - StartAngle) * (Timer / 100) + StartAngle

     //Test this working fine
    //circleTimer.addArcWithCenter(CGPointMake(50, 50), radius: CGFloat(130), startAngle: 0.0, endAngle: Progress, clockwise: true)

    circleTimer.addArcWithCenter(CGPointMake(200, 200), radius: CGFloat(130), startAngle: StartAngle, endAngle: Progress, clockwise: true)

    Timer--;
    circleTimer.lineWidth = 20;
    UIColor.redColor().setStroke()
    circleTimer.stroke()
}

Upvotes: 2

Related Questions