简体   繁体   中英

How to transition between views programatically?

I am developing a game, and I would like to transition between several views, eg Menu Screen, Game Screen, Game Over Screen etc. What would be the easiest way of doing this? I'm not sure if I should use a view stack as the order that the views are shown is not always reversed.

I assume by "view stack" you mean a UINavigationController?

The easiest way is to keep references to all of the view controllers somewhere, for example I see people use the application delegate a lot, so your application delegate's class extension would look a little like:

@interface AppDelegate ()

@property (nonatomic, strong) UIViewController *rootViewController; //this is what gets set as the window's root VC
@property (nonatomic, strong) UIViewController *mainScreenViewController;
@property (nonatomic, strong) UIViewController *gameScreenViewController;
@property (nonatomic, strong) UIViewController *gameOverScreenViewController;

@end

Assume rootViewController just controls a container view for the rest of the app (You would probably actually be well served putting all this logic into the root view controller though...)

Now anytime you need to show a certain screen, call a method like:

- (void)switchToViewController:(UIViewController *)viewController
{
    [self.rootViewController.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.rootViewController.view addSubview:viewController.view];
}

You can now write methods that are named more memorably like -switchToGameOverScreen

- (void)switchToGameOverScreen
{
    [self switchToViewController:self.gameOverScreenViewController];
} 

This basic pattern of view navigation is roughly found in UITabBarController and often in views controlled by UISegmentedControl s.

Hopefully this helps!

If you are developing a game in Cocos-2d,the transitions between scene can be done like this:

[[CCDirector sharedDirector] replaceScene:[CCTransitionCrossFade transitionWithDuration:0.5f scene:[GameOver scene]]];

Here GameOver is a new scene ,to which it goes after transition effect.

There are many other transitions in cocos-2d for going from one scene to another ie 0. CCTransitionCrossFade 1. CCTransitionFade 2. CCTransitionFadeBL 3. CCTransitionFadeDown 4. CCTransitionFadeTR 5. CCTransitionFadeUp 6. CCTransitionFlipAngular 7. CCTransitionFlipX 8. CCTransitionFlipY 9. CCTransitionJumpZoom 10. CCTransitionMoveInB 11. CCTransitionMoveInL 12. CCTransitionMoveInT 13. CCTransitionPageTurn 14. CCTransitionRadialCCW 15. CCTransitionRotoZoom 16. CCTransitionShrinkGrow 17. CCTransitionSlideInB 18. CCTransitionSlideInL 19. CCTransitionSlideInR 20. CCTransitionSlideInT 21. CCTransitionSplitCols 22. CCTransitionSplitRows 23. CCTransitionTurnOffTiles 24. CCTransitionZoomFlipAngular 25. CCTransitionZoomFlipX 26. CCTransitionZoomFlipY

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM