On my main menu I have a simple button layout and a couple sprites that move in the background to make it more interactive. When I first run it, everything's fine and the sprites move smoothly, and if I click on one of the menu's buttons, a custom segue is called, and the next view controller is presented exactly how I want it. Then I can press a back button, the menu is presented again, and everything is (mostly) good.
However if I repeat this process (spamming the navigation system), the segues still work, but eventually the sprites on the menu page start lagging and move in sharp jumps rather than smooth slides. So I think there's a problem with my segue implementation
Sample code:
class SegueFromLeft: UIStoryboardSegue {
override func perform() {
let src = self.source
let dst = self.destination
if let c = src as? LSViewController {
if let d = dst as? LSViewController {
d.page = (c.page + c.header.count - 1) % c.header.count
}
}
src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
dst.view.transform = CGAffineTransform(translationX: -src.view.frame.size.width, y: 0)
UIView.animate(withDuration: 0.25,
delay: 0.0,
options: UIViewAnimationOptions.curveEaseInOut,
animations: {
dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
},
completion: { finished in
src.present(dst, animated: false, completion: nil) }
)
}
}
Is there something wrong with how I'm handling the transition, or does the problem lay somewhere else (if so, what are some possible reasons)?
UPDATE: So I have SKScenes attached to each of the view controllers. That's where I attach the sprites to. And I think that has something to do with it...
The problem here is that you're not dismissing the SKScenes before the segue. They actually just keep running in the background. To fix this, call the following code somewhere during the segue:
scene.removeFromParent()
scene.view?.presentScene(nil)
This closes the scenes and actually stops the sprites.