vendredi 10 juin 2016

Unknown Crash with Animation


I'm tracking a fast moving object and plotting the relative x,y or z position within a UIView with a round UIView. Also, I have added a trailing smaller dot to show a "trace". Here is the maid code:

func plotter (type: NSString, xvalue: Float, yvalue: Float, zvalue: Float, vs: Int, scale: Float){

    //Scale factor
    var xorigin : Float = 0
    var yorigin : Float = 0

    switch vs {
    case 0:
        xorigin = (xvalue / (scale / (Float(plotterArea.frame.width/2)))) + Float(plotterArea.frame.width/2)
        yorigin = (yvalue / (scale / (Float(plotterArea.frame.height/2)))) + Float(plotterArea.frame.height/2)
    case 1:
        xorigin = (xvalue / (scale / (Float(plotterArea.frame.width/2)))) + Float(plotterArea.frame.width/2)
        yorigin = (zvalue / (scale / (Float(plotterArea.frame.height/2)))) + Float(plotterArea.frame.height/2)
    case 2:
        xorigin = (yvalue / (scale / (Float(plotterArea.frame.width/2)))) + Float(plotterArea.frame.width/2)
        yorigin = (xvalue / (scale / (Float(plotterArea.frame.height/2)))) + Float(plotterArea.frame.height/2)
    case 3:
        xorigin = (yvalue / (scale / (Float(plotterArea.frame.width/2)))) + Float(plotterArea.frame.width/2)
        yorigin = (zvalue / (scale / (Float(plotterArea.frame.height/2)))) + Float(plotterArea.frame.height/2)
    case 4:
        xorigin = (zvalue / (scale / (Float(plotterArea.frame.width/2)))) + Float(plotterArea.frame.width/2)
        yorigin = (xvalue / (scale / (Float(plotterArea.frame.height/2)))) + Float(plotterArea.frame.height/2)
    case 5:
        xorigin = (zvalue / (scale / (Float(plotterArea.frame.width/2)))) + Float(plotterArea.frame.width/2)
        yorigin = (yvalue / (scale / (Float(plotterArea.frame.height/2)))) + Float(plotterArea.frame.height/2)
    default:
        break
    }

    //tracerBall
    let tracerBall = UIView()
    tracerBall.frame = CGRect(x: CGFloat(xorigin-2.5), y: CGFloat(yorigin-2.5), width: 5, height: 5)
    tracerBall.layer.cornerRadius = 2.5
    tracerBall.backgroundColor = UIColor.darkGrayColor()

    dispatch_async(dispatch_get_main_queue(), {

        UIView.animateWithDuration(0.5, delay: 0, options: .BeginFromCurrentState, animations: {

            self.plotterAreaLabel.text = ("(type):: x:(String(format:"%.0f",xvalue )) y:(String(format:"%.0f",yvalue )) z:(String(format:"%.0f",zvalue ))")

            self.bouncingBall.frame.origin.x = CGFloat(xorigin-2.5)
            self.bouncingBall.frame.origin.y = CGFloat(yorigin-2.5)

            }, completion: { finished in
                self.plotterArea.addSubview(tracerBall)
                self.plotterArea.sendSubviewToBack(tracerBall)
                UIView.animateWithDuration(5.0, delay: 0.5, options: .CurveEaseInOut, animations: {

                    tracerBall.backgroundColor = UIColor.clearColor()

                    }, completion: { finished in
                        self.plotterArea.setNeedsFocusUpdate()
                })
        })

    })

}

The case statement is only there for a choice of x,y,z plots.

After an undetermined time (30/40 seconds) with a sample rate of 50Hz the programme crashes. I assume that a buffer is filling up, but unsure where and whether what I am doing is best practice.

Error seems to point o this line

 tracerBall.frame = CGRect(x: CGFloat(xorigin-2.5), y: CGFloat(yorigin-2.5), width: 5, height: 5)

enter image description here

Please help....


Aucun commentaire:

Enregistrer un commentaire