动画未知崩溃

时间:2016-06-07 21:22:41

标签: ios swift

我跟踪一个快速移动的物体,并用UIView绘制UIView内的相对x,y或z位置。另外,我添加了一个尾随的小点来显示" trace"。这是女仆代码:

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

    })

}

case语句仅用于选择x,y,z图。

在未确定的时间(30/40秒)后,采样率为50Hz,程序崩溃。我认为缓冲区正在填满,但不确定我在做什么,以及我在做什么是最好的做法。

错误似乎指向此行

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

enter image description here

请帮忙......

1 个答案:

答案 0 :(得分:0)

看起来我遇到了太多UIViews的限制。但是改为CAShapelayer它可以作为设计。