High CPU Usage because of UIView.animate and UIView.animateKeyframes

Viewed 560

I have two view controllers. AnimationVC has some UIView animations, and DestinationVC has none.

I have a CPU usage problem. After I perform the segue, the animation blocks still show up in the Instruments, even though these lines belong to the AnimationVC that performs the segue.

   UIView.animate(withDuration: 1.0, delay: 0.0, options: [.curveEaseInOut, .autoreverse, .repeat], animations: {
            self.s1.alpha = 0.0
            self.s3.alpha = 0.0
        }, completion: nil)

and

    let dur = 0.5/12
    UIView.animateKeyframes(withDuration: 30.0, delay: 0.0, options: [.repeat, .calculationModeCubic], animations: {
        UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: dur, animations: {
            self.sc.alpha = 1.0
        })

        UIView.addKeyframe(withRelativeStartTime: 1.0-dur, relativeDuration: dur, animations: {
            self.sc.alpha = 0.0
        })

        UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 1.0, animations: {
            self.sc.transform = CGAffineTransform(rotationAngle: .pi*0.2)
        })

        UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 1.0, animations: {
            self.sc.center.y = 0.1*self.frame.size.height
        })
    }, completion: nil)

I tried calling this destruct function...

func destruct(){
    layer.removeAllAnimations()
    subviews.forEach { $0.removeFromSuperview() }
}

...in

  1. just before performing the segue
  2. in the AnimationVC's deinit method

Still, it shows alive and hogs the CPU with 40% load. How can I destruct this? I even reset the navigation stack on the DestinationVC with...

self.navigationController?.viewControllers = [self]

...and I see the lines of deinit from AnimationVC, the animation delay closure is still alive.

From Instruments

1 Answers
Related