0

Hi I have simple CAShapeLayer animation

   let ballFrameAnimation  = CAKeyframeAnimation()
   aBall.removeFromSuperlayer()

  self.view.layer.addSublayer(aBall)

  ballFrameAnimation.keyPath =  "position"
  ballFrameAnimation.duration = 3.0
  ballFrameAnimation.calculationMode = kCAAnimationDiscrete  
  ballFrameAnimation.fillMode = kCAFillModeForwards
  ballFrameAnimation.isRemovedOnCompletion  = true
  ballFrameAnimation.delegate = self

with a callback to keeps it repeating

func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {

  let name = anim.value(forKey: "name") as? String
  if name == "form" {
      attachAnimation()
       forwardDirection = !forwardDirection

  }

However this persists and loops in the background ( at high speed) even when I pop the viewcontroller off the stack. It also shows a memory leak as well on instruments.

 @IBAction func unwindDismiss(_ sender: Any) {

 aBall.removeAnimation(forKey: "ballAnimation")
 aBall.removeFromSuperlayer()
navigationController?.popToRootViewController(animated:true)
dismiss(animated: true, completion: nil)
}

1 Answers1

0

It was simple.

The endless loop animation would not be released by simply dismissing the viewcontroller so it persisted in the background.

By adding a

if navigationController?.topViewController == self {

loop check within the animation did stop cycling. this fixed not only the accelerating background loops but also the memory leaks. also a warning to fellow NOOBs : when using the instruments repeatedly: close and restart them frequently and also clean builds otherwise you are likely to get bogus leaks.