Kalzem
Kalzem

Reputation: 7501

Scene transition of SKScene with UIKit elements (UIButtons, UILabel, ...)

I have a menu which is a SKScene with a SKSpriteNode as as background and with 3 UIButtons and 1 UILabel.

When the user clicks the button, this scene transition is triggered :

view?.presentScene(GameScene(size: view!.bounds.size), transition: SKTransition.fadeWithDuration(2))

The scene does change (the background changes) but all my UIElements are still in place. Is there a proper way to remove them along the scene transition ? By proper way I mean that the garbage collector should at some point free the menu scene from the memory (which cannot be done if there are still UIElements on it).

Upvotes: 1

Views: 876

Answers (2)

ZeMoon
ZeMoon

Reputation: 20284

You have two options here. First is to keep using the UIKit elements and actively removing them when transitioning to your game scene. For example, you can set up a method for doing the same when the button for the game is pressed:

func hideButtons () {
    //Hide your buttons and label here.
    btn.hidden = true //Either hide it
    btn.removeFromSuperview() //or remove it altogether.
    ... 
}

And then call this method just before you present the scene.

self.hideButtons()
view?.presentScene(GameScene(size: view!.bounds.size), transition: SKTransition.fadeWithDuration(2))

Alternatively, you can implement a button in the SKScene itself, using the touch delegates.

Declare a SKSpriteNode with the same frame as your button. When you detect a touch on the button, trigger the scene change.

Another option is using a class like AGSpriteButton. It has a setup system very similar to that of a UIButton.

Upvotes: 1

nacross
nacross

Reputation: 2043

If you want the buttons to be released then you will have to remove them from the view.

It is worth considering though if this really necessary? Is the amount of memory reclaimed worth the additional cost to re-add these buttons later? assuming you can return to the menu.

There are a few options for making a visually appealing transition using both SpriteKit and UIKit elements. Although I wouldn't necessarily recommend them.

  1. Animate the buttons off screen manually, by adjusting the frame or alpha.
  2. Remove the transition from the SKScene and animate a transition of the UIView using transitionWithView:duration:options:animations:completion: or transitionFromView:toView:duration:options:completion:

If you can't create an effect using either of these options that is satisfactory then it is probably worth reimplementing your scene/view to use only SpriteKit elements.

Upvotes: 1

Related Questions