I am developing a puzzle game for the iPhone using core graphics implementing drawrect in a single UIView.
I would like to add a menu which is opened from a UIButton event handler.
I am not sure if I should do this via code e.g. have a menuopen flag and adjust the drawing routines and tap event handlers (manual implementation) or to go the UINavigationController route and have a completely separate view which would be activated i assume in the UINavigationController from the button event.
Currently my music and sound stuff is in a class attached to the "main" UIView in my game and initialised via ViewDidLoad.
Any advice would be greatly appreciated.
You should definitely put your menu code in a different view object. Don't just redraw the existing view in menu mode. That will make your code needlessly complicated.
You don't have to use UINavigationController to swap out one view for another, however. You write code yourself to remove the game view (removeFromSuperview) and add the menu view (addSubview:) to the application's window, using transition animations if you want.
You could also use UIViewController's presentModalViewController: method to push your menu view on top of the game view.
It's hard to say exactly what you need to do without knowing more about your code, but you should absolutely definitely keep different things as different classes, instead of making one class that acts as many things depending on a mode flag.
Related
Simple question, I've done a decent amount of exploration around custom navigation / transitions between UIViewControllers and am unclear about the following:
I'm looking for behavior similar to what UIPageViewController provides (non stack-based navigation forward and backwards through "pages" of content). But I want to be able to customize the transitions, and I want the transitions to be interactive linked to a custom UIPanGestureRecognizer.
It seems like the UIViewControllerInteractiveTransitioning protocol provides some of what I want (interactivity, custom transitions). But because transitions are invoked solely with presentViewController:animated: and dismissViewControllerAnimated: it seems like it's built exclusively for use with stack-based navigation (ie UINavigationController, UITabBarController, modal presentation). Ie it doesn't seem like it'll play nice with something like UIPageViewController.
If I use UIViewController containment to build a custom container similar to UIPageViewController (see in progress demo here) can I integrate the UIViewControllerInteractiveTransitioning protocol into this to drive the transitions? Or do I need to roll those on my own (currently I have a rough manual implementation of interactive transitions)?
I do a lot of custom animations in my apps that most developers shy away from because I use a lot of UIViewControlloer Containment in my work.
It's the simplest way to get the transitions that you're looking for.
Here's how I would go about it:
Create a base view controller; lets call it MainViewController. It will have references to all of the other view controllers and hold the logic for the transitions. It should also follow a protocol we'll define as ViewXControllerDelegate.
Create your other view controllers; lets call them View1Controller, View2Controller, View3Controller. Add an instance of each of them as private properties of MainViewController. In the init method of MainViewController, instantiate them and add their views as subviews of MainViewController's view. Should look something like this:
self.v1c = [[View1Controller alloc]init];
[self addChildViewController:self.v1c];
[self.v1c didMoveToParentViewController:self];
//Setup each subview so that its frame makes it off screen or
//On screen depending on the app state and where you want each
//subview to animate to/from
[self.view addSubview:self.v1c.view];
....
Setup up a UIPanGestureRecognizer in each of your ViewXControllers that has its target and selector set to the parent view controller (MainViewController).
Handle all logic in your MainViewController class where you take the distance swiped, application state, the locations of each of the views into account (using helper properties in the ViewXControllers like "inactiveFrame" or "activeFrame" where the animation between them happens based on the percentage of movement that's occurred in the pan gesture.
I'm trying to build an educational app that will have approximately 3-5 completely different pages/screens. Each screen contains one puzzle and each puzzle is independent of all the other puzzles (screens). Once the puzzle on the current screen is solved I'd like to transition to a new (randomly selected) screen. I would allow the same puzzle to be shown multiple times, however, if it had been displayed before it would need to be reset.
I've tried doing this using segues but that seems to require a UINavigationController which is not the experience I want to present (since it requires a predefined hierarchy of screens).
I assume the best way to do this is to have each screen as completely separate UIViewControllers, correct? I'm just not sure how to orchestrate the navigation/rendering between them.
I'd appreciate whatever best practices you know of. Thanks!
Well here is a possible option:
First of all if you want a menu outside of these puzzles have that as your root view controller in a UINavigationController.
Then create a launcher controller and add it to the navigation controller when appropriate. This will be a regular UIViewController except in this controller in the viewWillAppear method have it pick a random number 0-5 and run it through a switch and depending on the number push one of your 5 view controllers. Then when the puzzle is completed call popViewController on the navigation controller. Now when it pops back, viewWillAppear will be called again and randomly push another. Then if you want to go back to the main menu at any time just call popToRootViewControllerAnimated. This should do what you need. And to make it so it doesn't animate twice either only have the push animate or only the pop.
Rather than having different view controllers you can have a single controller. You can reload the view of the puzzle after completing the first. The logic you have applied for the first puzzle will be some what similar to the the others. So now only you need to handle different states of the puzzle.
For example: I have first view with puzzle 3x3 matrix, the next will be 4x4 matrix and the next will be 5x5 matrix. So this states needs to be handled through code in view controller.
I know that using cocos2d is a great way to build games and it allows different "scenes" where things are independent of each other or can communicate if you choose. Also with cocos2d there is a "director" that handles all of the scenes and can push scenes for you. If you are curious check out: www.cocos2d-iphone.org
I hope this helps :)
Well, I was build the app tested yesterday to play different music instrument. I use UIView to handle view for 3 of my instrument. The logic is simple. I put the navigation on UIButton and add a subview for each instrument. When the user touch the button, the selected view will be add to the front, and the last view will be release or temporary on the background.
Hope it will help you.
I've started developing my first game. Here some my thoughts about architecture:
Most examples use the project template based on single view and manage of control visibility by hand. In my game I've 6-7 views and I'd like use Interface Builder for some of them. There are some problems too:
If I use a single based view I'll need, for example, in "Play" button's handler to create new view with a game board and destroy previous one (the view contains that button's handler). Is it right and how can I do it?
If I use a navigation based view I'll use push/pop methods but I'll need to hide a navigation bar for game atmosphere and to do navigation by hand. Moreover, I couldn't find any example of use a navigation controller in games. Does anybody use it and how?
What can you advice me?
1) you can hide navigation bar from nib file.
2) you can remove navigation animation by setting animation by [self pushViewController:nav animated:NO];
3)if you do not want to use navigation controller , you can use view based application and through coding you can add/remove subviews and views as per need.
I want to implement a view on which has "resume", "save" and "quit" buttons and it popups when the game is started or interrupted. I looked up a few books and most just mentioned animation of view's transition or suggested using tab bar controller, etc and all these are not applicable to my game's need. I would like to know:
1) How to implement it? Such as how to declare, how to call and and where it is called? Just create another ViewController class with .xib file? Then what? Just add it as a subview to window in delegate file like [window addSubview:viewController.view];?
2) How to make it appear after the startup intro screen and before the game starts?
3) How to make it appear on top of the game when the game is paused?
4) How to make it disappear when it is resumed?
I would create a separate view controller and display it with -presentModalViewController:animated:.
I'm learning how to develop my own iPhone apps but I'm having a tough time understanding certain concepts.
First, am i right to say that for every view, there must be a view controller for it? And for every view controller, must there be a delegate for it?
Also, what is the role of mainWindow.nib? Most of the tutorials that i've read don't seem to touch that nib at all. What always happens is the setting up of a NavigationController as the root controller, which pushes another ViewController onto the stack and this ViewController will have another nib associated with it.
So can i assume that i can safely ignore the main window nib?
It's all about MVC (Model View Controller), innit?
The Model, well that's up to you - what does your app do? Think of it as the backend, the engine of your app, free of the cruft of font size decisions and touch events.
The View, Apple pretty much wrote that for you. You use their Textfields and tables and imageViews. You assemble them together using Interface Builder into your GUI (packaged as a .nib). You rarely, if ever need to subclass the standard view elements (in a game you want a custom View to draw to, as all your drawing is probably custom). You can break different parts of your GUI into different .nib files if this helps you manage them. It's entirely up to you.
The Controller, so you have probably got some work todo to enable your GUI to represent your model. You need Some Controllers. How many? However many is manageable by you. If you had a view containing 2 subviews would they each need a view controller? Nah, probably not. How complicated is your code to hook up the view to the model?
Some GUI patterns are so common that Apple even wrote the Controller code for you. EG the controller for a UINavigationBar, UINavigationController. So, if your app has hierarchical views that you need to navigate around and you need to display a navigation bar you can use an instance of UINavigationController instead of writing your own class. Yay!
Surely tho, the UINavigationController code (or any other viewController) can't magically know how to integrate with our model, with our view, can it? NO, it can't. In general in Cocoa if there is some class of object that mostly works off the shelf but also has optionally configurable behavoir - allowing us to tailor it to our needs - it is done by Delegation. ie Instead of subclassing UINavigationController we tell the specific instance of it where to find (for want of a better term) it's custom behavoir.
Why? Let's say you have a navigationController, a tableView and a textfield. UINavigationController mostly take care of your navigation needs but you have to have a crazy QUACK sound play each time the user moves to a new view. UITableView is mostly exactly everything you need from a table, EXCEPT you really want the third row in the table on the front page be twice the height of the other rows. And the standard, off -the-shelf UITextField pretty much takes care of your textfield needs EXCEPT you need your textfield to only be editable when the user is facing North. One way to handle this would be to create 3 new classes, a custom UINavigationController, a custom tableView and a custom textfield, and to use these instead. With delegation we could use the classes as they are and have one object be the delegate of all 3 instances - much cleaner.
Delegation is mostly optional, the docs will tell you when, and it's down to you and whether you need that custom behavoir.