I am developing a game, and I would like to transition between several views, e.g. 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 UISegmentedControls.
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 i.e
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
Related
TLDR
Properties are not showing on a UIViewController that I was able to access when working on this project yesterday. This is not as simple as how do I set a property on a VC? I can not see the properties of the VC.
The Situation
A custom UIViewController (A) pushes to a second UIViewController (B), pushes to a third UIViewController (C)
//programmatically calling a push to the next view in A to B to C, example code from B to C.
EnterFinalHRViewController * finalHeartRate = [self.storyboard instantiateViewControllerWithIdentifier:#"EnterFinalHeartRateController"];
finalHeartRate.lanes = _lanes;
[self.navigationController pushViewController:finalHeartRate animated:YES];
The NSMutableArray, lanes, gets passed along twice. Not a problem.
When I arrive in VC(C) the debugger reads:
A self = (EnterFinalHRViewController *) #"0 objects" 0x15da40c0
V [0]
I have a header file in VC(C) that reads
#import <UIKit/UIKit.h>
#import "SwimTimingViewController.h"
#import "Lane.h"
#interface EnterFinalHRViewController : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource, UIPickerViewDataSource, UIPickerViewDelegate>
#property (nonatomic, strong) NSMutableArray *lanes;
#end
For Clarity
Lanes is just an example of one property that is passed along.
This is not just trying to set and access a property of a VC across a segue
I've looked everywhere I can, any more links would be appreciated as well.
The Question
Why can't I see any properties of self in the final UIViewController in the debugger, how would one fix it, and or any useful debugging tips.
My Theory
Since upgrading to a new version of xcode, last night, something broke / xcode bug, and I don't know what that is. I am now using Xcode Version 6.1 (6A1052d). I am testing using my iPhone 5c running ios7. (I will be upgrading as soon as everything works 100% in ios7).
Quick shout out to thank all the contributors here at SO and beyond. The help is alway very warmly welcomed.
This behavior is very odd. Every time I create a new project in Xcode 5.0.2 I cannot get it to work fully. Once the Single View Application template (or another) is created - everything seems OK: I have my delegate, storyboards and ViewController. They all run successfully and I can even see newly added UIView's in my Simulator.
Her is my generic code for ViewController:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController{
UIView *container;
}
#property (nonatomic,retain) IBOutlet UIView *container;
#end
#import "ViewController.h"
#interface ViewController (){}
#end
#implementation ViewController
#synthesize container;
- (void)viewDidLoad{
[super viewDidLoad];
NSLog(#"App loaded");
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
}
#end
But once I add properties to this controller class and synthesize them, the screen turns black during app running and no views or whatever was added before is ever seen anymore. Just a blank black color screen.
Where is the problem? Please note:
I checked Info*.plist and it points to Storyboard correctly
In delegate my didFinishLaunchingWithOptions returns YES
ViewController has a Initial View Controller checked and is the only one in the storyboard
viewDidLoad prints NSLog message successfully even though nothing is shown
What to check?
Here is a link to the project
A view controller is just that, a controller. The UIViewController's view property is what is visible on the screen (assuming you added it to the screen at some point, storyboard, programatic, xib, etc...)
Check the UIViewcontroller.view.superview. You should be able to get close to what you want.
The view controller properties thing is a complete red herring.
You're using a storyboard for your app, which has all of the relevant details in it. Then, in your app delegate, you've added a load of code to load in a new instance of the view controller and set it as your app's root view controller.
You don't need to do this if you have a storyboard. Just return YES from applicationDidFinishLaunching.. (which was originally the case, since you included the git history in the download!). The initial view controller from your storyboard will already be loaded up. What you've done is to basically ignore the storyboard.
I'm trying to get MMDrawerController to work, and I'm having trouble.
Here's is how much app is structured in my storyboard:
Here's how I'm attempting to initialize it from within my root view controller:
//LCViewController.m
#import "LCViewController.h"
#import "MMDrawerController.h"
#interface LCViewController ()
#property (nonatomic,strong) MMDrawerController * drawerController;
#end
#implementation LCViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.drawerController = [[MMDrawerController alloc]
initWithCenterViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"centerNav"]
leftDrawerViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"menu"]
rightDrawerViewController:nil];
}
...
#end
When I build my app, all I see is my root view controller. Is there something else I'm supposed to do to implement the drawer functionality?
I created a demo project to show how I'm trying to set up my app. You can download the Xcode workspace here. Thanks in advance for your help!
I'm using Xcode 5 and iOS 7
EDIT: Sorry I initially misunderstood your app structure. The MMDrawerController should be the root view controller of your application. You should move this code from viewDidLoad to application:didFinishLaunchingWithOptions:. Add an MMDrawerController property to your app delegate, init the drawer controller with your appropriate views, and set the drawer controller to the rootViewController on your UIWindow. Do this along with setting the gesture modes as I described below and the drawer should work.
To get the basic open/closing gestures, set this properties on your drawer controller:
self.drawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModeAll;
self.drawerController.closeDrawerGestureModeMask = MMCloseDrawerGestureModeAll;
These properties default to MMOpenDrawerGestureModeNone which is why you couldn't make anything slide. You can have a look at the MMOpenDrawerGestureMode and MMCloseDrawerGestureMode bitmasks to get finer grained settings if you desire.
You can also create UI controls that toggle the drawer by calling toggleDrawerSide:
animated:
completion:.
I am trying to create an application with a feature similar to facebook's chat bubbles.
When the user navigates to a certain page (InCallViewController), they can connect to another person via video chat. When they navigate out of this page after connecting, I would like the video view to stay floating on the screen, but allow them to do what ever they want to do in the app.
In order to do this, I have made an InCallViewController class, which will allow the user to connect with the other person. Once connected, the video is displayed in a view. This view is movable (similar to facebook's chat bubbles) and displays the video chat perfectly, however when I exit the page and go to another page (AccountViewController) in the app, I am unable to keep this view on the screen. I have tried many things, including setting this view as a subview in the later pages. However when I do this, the subview is not displayed.
MyAccountView.m
- (void)viewDidLoad
{
[super viewDidLoad];
InCallViewController *inCallViewController = [[InCallViewController alloc] initWithNibName:#"InCallViewController" bundle:nil];
[self.view addSubview:inCallViewController.previewView];
[self.view bringSubviewToFront:inCallViewController.previewView];
(Do some other set up stuff)
}
InCallViewController.h
#interface InCallViewController : UIViewController <UIAlertViewDelegate>
{
CGPoint currentTouch;
NSArray *viewArray;
AVAudioPlayer *audioPlayer;
}
#property (weak, nonatomic) IBOutlet UIView *previewVideoView;
The previewView is a UIView in the InCallViewController class. This is hooked up in the IB, and works perfectly when in the InCallController class. The problem is, it won't show up when adding it as a subview in another class. I am wondering what I am doing wrong, or if there is a better way to keep the "previewView" remaining on the screen after I exit InCallViewController.
Thanks
You should consider implementing a container viewController. Since iOS6 and xcode 4.5 this has been made pretty straightforward.
The containing viewController can be handling your previewViews which are overlayed over whatever viewController is currently contained in it.
You can compare what you want to achieve with what Apple has achieved with a UInavigationController (also a container view controller): it contains viewController that are happily showing their content, but the navigationController makes sure the navigationBar is always present, for all viewControllers, even during animations.
Apple has some good documentation and even a WWDC session on this.
Hacking your way into [[UIApplication sharedApplication] keyWindow] is extremely poor design, and a blatant violation of the MVC pattern. It works, but it is a hack nonetheless and might give you headaches in the future.
You can add previewView to [[UIApplication sharedApplication] keyWindow] as a subview so that it appears on all your views and above each of them.
My problem is the following: I want to only allow Portrait orientation on all my ViewControllers except 1 ViewController which is supposed to allow both Portrait and landscapeLeft/Right. I have now spent almost 2 days into how to set orientation in IOS for different slides/ViewControllers. After some searching I found this thread here at stack: UITabBarController Rotation Issues in ios 6
I followed Kunani's example in that thread which I will post here to save all readers some time:
Zack, I ran into this same issue. It's because you have your viewController embedded inside of a TabBar Controller or UINavigationController and the calls to these methods are happening inside those instead of your normal View (Changed in iOS6). I ran into this issue because I was presenting a viewController embedded inside a UINavigationController on all my modal views that had Navigation to different views (Signup Process, Login, etc). My simple fix was to create a CATEGORY for UINavigationController that includes these two methods. I have shouldAutorotate returning NO anyway because I don't want my modal views rotating. Your fix may be this simple, give it a try. Hope it helps. I created a category and named it autoRotate and selected theUINavigationController option. The M+H file are below.
#import "UINavigationController+autoRotate.h"
#implementation UINavigationController (autoRotate)
-(BOOL)shouldAutorotate {
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
#end
... and the category .h:
#import <UIKit/UIKit.h>
#interface UINavigationController (autoRotate)
-(BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations;
#end
I did what he said and tried to set category for my UITabBarController which worked, all classes connected to the tabBar now only allows orientationPortrait. But if you look at the following Picture
(screenshot from my project) there is a class in the middle of the StoryBoard called ShowTaskView. This class is connected to most classes (which are directly connected to the UITabBarController) via a UINavigationController. Even if I set UITabBarController to only allow Portrait also ShowTaskView seems to get affected by that rule and I can not make it to rotate. The scheme in my project can also be described as this:
TabBarController ----> UINavigationController -------> class X ----------> class ShowTaskView
What can I do from here if I want my classes connected to tabBarController only to allow orientationPortrait and the rest of the classes allow both portrait and landscape based on how my project is built? I am very frustrated at this issue since it is so damn hard to solve :/
Regards
please refer to my answer in similar thread: Navigation controller stack in landscape mode, but modally presented view controller view always in portrait frame size
iOS6 controls rotation by the navigation stacks, so wrap your rotatable view into separate navigation controller to be able to control it there.