iOS Developer Library

Developer

SpriteKit Framework Reference SKTransition Class Reference

Options
Deployment Target:

On This Page
Language:

SKTransition

Inheritance


Conforms To


Import Statement


Swift

import SpriteKit

Objective-C

@import SpriteKit;

Availability


Available in iOS 7.0 and later.

An SKTransition object is used to perform an animated transition between a SKScene object already presented by an SKView object and a new incoming scene.

Scenes are the basic building blocks of games. Typically, you design self-contained scenes for the parts of your game, and then transition between these scenes as necessary. For example, you might create different scene classes to represent any or all of the following concepts:

  • A loading scene to display while other content is loaded

  • A main menu scene to choose what kind of game the user wants to play

  • A scene to configure the details of the specific kind of game the user chose

  • A scene that provides the gameplay

  • A scene displayed when gameplay ends

When you present a new scene in a view that is already presenting a scene, you have the option of using a transition to animate the change from the old scene to the new scene. Using a transition provides continuity so that the scene change is not quite so abrupt.

Transitioning Between Two Scenes

Typically, you transition to a new scene based on gameplay or user input. For example, if the user presses a button in your main menu scene, you might transition to a new scene to configure the match the player wants to play. Transitioning to a new scene shows how you might implement the event handler in a sprite. The handler first runs an animation on itself to highlight the button (not described here). Then, it creates a transition object and the new scene. Finally, it calls the view to present the new scene. The transition means that this change is animated.

Listing 1Transitioning to a new scene
  • - (void)mouseUp:(NSEvent *)theEvent
  • {
  • [self runAction: self.buttonPressAnimation];
  • SKTransition *reveal = [SKTransition revealWithDirection:SKTransitionDirectionDown duration:1.0];
  • GameConfigScene *newScene = [[GameConfigScene alloc] initWithSize: CGSizeMake(1024,768)]];
  • [self.scene.view presentScene: newScene transition: reveal];
  • }

When the transition occurs, the scene property is immediately updated to point to the new scene. Then, the animation occurs. Finally, the strong reference to the old scene is removed. If you need to keep the scene around after the transition occurs, your app needs to keep its own strong reference to the old scene.

When organizing your game, it can be helpful to create a diagram that shows all the scenes in a game, the transitions that occur between scenes, and the data that must be passed to the new scene when a transition occurs. Unlike view controllers in iOS, Sprite Kit does not provide a built-in mechanism for passing data between scenes. If you need to provide data during a scene transition, you need to implement your own mechanism to configure the new scene. Typically, this means defining custom methods and properties on each scene.

Configuring Whether Animations Play During the Transition

The pausesIncomingScene and pausesOutgoingScene properties on the transition object define which animations are played during the transition. By default, both scenes are paused during the transition. However, you might want to enable animation on either scene during the transition. For example, consider the code again in Transitioning to a new scene. Because the button is going to run an action, this code expects the outgoing scene to be animated. But perhaps the incoming scene should not animate its content until the transition completes. Adding the code in Pausing frame processing during a transition has the desired effect.

Listing 2Pausing frame processing during a transition
  • reveal.pausesOutgoingScene = YES;
  • reveal.pausesIncomingScene = NO;
  • Creates a cross fade transition.

    Declaration

    Swift

    class func crossFadeWithDuration(_ sec: NSTimeInterval) -> SKTransition

    Objective-C

    + (SKTransition *)crossFadeWithDuration:(NSTimeInterval)sec

    Parameters

    sec

    The duration of the transition.

    Return Value

    A new transition.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Creates a transition where the new scene appears as a pair of closing horizontal doors.

    Declaration

    Swift

    class func doorsCloseHorizontalWithDuration(_ sec: NSTimeInterval) -> SKTransition

    Objective-C

    + (SKTransition *)doorsCloseHorizontalWithDuration:(NSTimeInterval)sec

    Parameters

    sec

    The duration of the transition.

    Return Value

    A new transition.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Creates a transition where the new scene appears as a pair of closing vertical doors.

    Declaration

    Swift

    class func doorsCloseVerticalWithDuration(_ sec: NSTimeInterval) -> SKTransition

    Objective-C

    + (SKTransition *)doorsCloseVerticalWithDuration:(NSTimeInterval)sec

    Parameters

    sec

    The duration of the transition.

    Return Value

    A new transition.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Creates a transition where the new scene appears as a pair of opening horizontal doors.

    Declaration

    Swift

    class func doorsOpenHorizontalWithDuration(_ sec: NSTimeInterval) -> SKTransition

    Objective-C

    + (SKTransition *)doorsOpenHorizontalWithDuration:(NSTimeInterval)sec

    Parameters

    sec

    The duration of the transition.

    Return Value

    A new transition.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Creates a transition where the new scene appears as a pair of opening vertical doors.

    Declaration

    Swift

    class func doorsOpenVerticalWithDuration(_ sec: NSTimeInterval) -> SKTransition

    Objective-C

    + (SKTransition *)doorsOpenVerticalWithDuration:(NSTimeInterval)sec

    Parameters

    sec

    The duration of the transition.

    Return Value

    A new transition.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Creates a transition where the previous scene disappears as a pair of opening doors. The new scene starts in the background and moves closer as the doors open.

    Declaration

    Swift

    class func doorwayWithDuration(_ sec: NSTimeInterval) -> SKTransition

    Objective-C

    + (SKTransition *)doorwayWithDuration:(NSTimeInterval)sec

    Parameters

    sec

    The duration of the transition.

    Return Value

    A new transition.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Creates a transition that first fades to a constant color and then fades to the new scene.

    Declaration

    Swift

    class func fadeWithColor(_ color: UIColor, duration sec: NSTimeInterval) -> SKTransition

    Objective-C

    + (SKTransition *)fadeWithColor:(UIColor *)color duration:(NSTimeInterval)sec

    Parameters

    color

    The color to use as the fade color.

    sec

    The duration of the transition.

    Return Value

    A new transition.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Creates a transition that first fades to black and then fades to the new scene.

    Declaration

    Swift

    class func fadeWithDuration(_ sec: NSTimeInterval) -> SKTransition

    Objective-C

    + (SKTransition *)fadeWithDuration:(NSTimeInterval)sec

    Parameters

    sec

    The duration of the transition.

    Return Value

    A new transition.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Creates a transition where the two scenes are flipped across a horizontal line running through the center of the view.

    Declaration

    Swift

    class func flipHorizontalWithDuration(_ sec: NSTimeInterval) -> SKTransition

    Objective-C

    + (SKTransition *)flipHorizontalWithDuration:(NSTimeInterval)sec

    Parameters

    sec

    The duration of the transition.

    Return Value

    A new transition.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Creates a transition where the two scenes are flipped across a vertical line running through the center of the view.

    Declaration

    Swift

    class func flipVerticalWithDuration(_ sec: NSTimeInterval) -> SKTransition

    Objective-C

    + (SKTransition *)flipVerticalWithDuration:(NSTimeInterval)sec

    Parameters

    sec

    The duration of the transition.

    Return Value

    A new transition.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Creates a transition where the new scene moves in on top of the old scene.

    Declaration

    Swift

    class func moveInWithDirection(_ direction: SKTransitionDirection, duration sec: NSTimeInterval) -> SKTransition

    Objective-C

    + (SKTransition *)moveInWithDirection:(SKTransitionDirection)direction duration:(NSTimeInterval)sec

    Parameters

    direction

    The direction of the move. Possible values are described in Transition Directions.

    sec

    The duration of the transition.

    Return Value

    A new transition.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Creates a transition where the new scene moves in, pushing the old scene out of the view.

    Declaration

    Swift

    class func pushWithDirection(_ direction: SKTransitionDirection, duration sec: NSTimeInterval) -> SKTransition

    Objective-C

    + (SKTransition *)pushWithDirection:(SKTransitionDirection)direction duration:(NSTimeInterval)sec

    Parameters

    direction

    The direction of the push. Possible values are described in Transition Directions.

    sec

    The duration of the transition.

    Return Value

    A new transition.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Creates a transition where the old scene moves out of the view, revealing the new scene underneath it.

    Declaration

    Swift

    class func revealWithDirection(_ direction: SKTransitionDirection, duration sec: NSTimeInterval) -> SKTransition

    Objective-C

    + (SKTransition *)revealWithDirection:(SKTransitionDirection)direction duration:(NSTimeInterval)sec

    Parameters

    direction

    The direction of the reveal. Possible values are described in Transition Directions.

    sec

    The duration of the transition.

    Return Value

    A new transition.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Creates a transition that uses a Core Image filter to perform the transition.

    Declaration

    Swift

    init(CIFilter filter: CIFilter, duration sec: NSTimeInterval) -> SKTransition

    Objective-C

    + (SKTransition *)transitionWithCIFilter:(CIFilter *)filter duration:(NSTimeInterval)sec

    Parameters

    filter

    A Core Image filter.

    sec

    The duration of the transition.

    Return Value

    A new transition.

    Discussion

    The filter used to perform the transition must a be filter that requires only two image parameters (inputImage, inputTargetImage) and generates a single image (outputImage). The transition automatically sets the filter’s inputImage, inputTargetImage, and inputTime properties. You must set up any other filter properties before creating the transition.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • A Boolean value that determines whether the incoming scene is paused during the transition.

    Declaration

    Swift

    var pausesIncomingScene: Bool

    Objective-C

    @property(nonatomic) BOOL pausesIncomingScene

    Discussion

    The default value is YEStrue.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • A Boolean value that determines whether the outgoing scene is paused during the transition.

    Declaration

    Swift

    var pausesOutgoingScene: Bool

    Objective-C

    @property(nonatomic) BOOL pausesOutgoingScene

    Discussion

    The default value is YEStrue.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • For some transitions, the direction in which the transition is performed.

    Declaration

    Swift

    enum SKTransitionDirection : Int { case Up case Down case Right case Left }

    Objective-C

    typedef NS_ENUM (NSInteger, SKTransitionDirection ) { SKTransitionDirectionUp, SKTransitionDirectionDown, SKTransitionDirectionRight, SKTransitionDirectionLeft, };

    Constants

    • Up

      SKTransitionDirectionUp

      The transition goes up.

      Available in iOS 7.0 and later.

    • Down

      SKTransitionDirectionDown

      The transition goes down.

      Available in iOS 7.0 and later.

    • Right

      SKTransitionDirectionRight

      The transition goes right.

      Available in iOS 7.0 and later.

    • Left

      SKTransitionDirectionLeft

      The transition goes left.

      Available in iOS 7.0 and later.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.