我想添加一個CAShapelayer每20ms一次給定的x和y座標。我希望這個形狀能夠在一秒之內消失(就像一個示蹤劑)。我創建的函數工作,形狀創建在正確的位置並消失。但是我得到了額外的形狀留在屏幕上。CAShapelayer重複
func shadowBall (x: CGFloat, y: CGFloat){
let xpos : CGFloat = ((self.frame.width/2) + x)
let ypos : CGFloat = ((self.frame.height/2) + y)
let shadowBall = CAShapeLayer()
let shadowBalllRadius :CGFloat = 4
let shadowBallPath : UIBezierPath = UIBezierPath(ovalInRect: CGRect(x: xpos, y: ypos, width: CGFloat(shadowBalllRadius*2), height: CGFloat(shadowBalllRadius*2)))
shadowBall.path = shadowBallPath.CGPath
shadowBall.fillColor = UIColor.clearColor().CGColor
shadowBall.strokeColor = UIColor.whiteColor().CGColor
shadowBall.lineWidth = 0.5
let animation: CABasicAnimation = CABasicAnimation(keyPath: "strokeColor")
animation.fromValue = UIColor.whiteColor().CGColor
animation.toValue = UIColor.clearColor().CGColor
animation.duration = 1.0;
animation.repeatCount = 0;
animation.removedOnCompletion = true
animation.additive = false
self.layer.addSublayer(shadowBall)
shadowBall.addAnimation(animation, forKey: "strokeColor")
}
無關,但我覺得「每20ms」是好奇地靠近(但不等於)60個FPS的最大幀速率。你使用'NSTimer'嗎?爲什麼不使用'CADisplayLink',它本質上是一個針對幀更新而優化的定時器? – Rob
我有一個刷新頻率最高爲50Hz的BLE設備...因此20ms – duck1970