How to scale a game designed for iPad to iPhone? - iphone

I made a game using cocos2d for iPad.. Now I have to scale it for iPhone. I tried using scaleX and ScaleY parameters for my GameScene. It scaled the sprites, but I have a lot of hard coded boundaries for my objects which don't get scaled. How can I do the complete scaling of my game??

you have to do something like following
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
[yourview setFrame:CGRectMake(400, 208, viewrect.size.width, viewrect.size.height)];
else
[yourview setFrame:CGRectMake(190, 96, viewrect.size.width, viewrect.size.height)];
you already done for iPad, so now you need to change else portion
set your view's frame there according to device orientation.

Related

How to convert from iPAD/iPAD2 layout to iPhone4/iPhone5/iPAD-mini layout

I never used Interface Builder ever in my app, and don't think it is something good to do.
My app was already designed for iPad/iPad2 layout, without ever considering making it work on iPhone4/iPhone5/iPAD-mini. Now I want my app to work on all of them.
I think it would be extremely ugly to have 4+ layout the universal app, like this:
if(ipad or ipad2)
view.frame = CGRectMake(0,0,100,100);
else if(ipad-mini)
view.frame = CGRectMake(0,0,100,100);
else if(iphone4)
view.frame = CGRectMake(0,0,100,100);
else if(iphone5)
view.frame = CGRectMake(0,0,100,100);
else if(iphone6)
view.frame = CGRectMake(0,0,100,100);
else if(iphone7)
view.frame = CGRectMake(0,0,100,100);
There should be only 2 layout ideally, but I don't know how to implement this way. Those fixed coordinates would not stretch on different screen sizes
if(ipad or ipad2 or ipad-mini)
view.frame = CGRectMake(0,0,100,100);
else if(iphone4 or iphone5 or iphone6 or iphone7)
view.frame = CGRectMake(0,0,100,100);
How do I do a universal app in the best way?
when you started building this app you should have done soemthing like this:
view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
and you change above options according to your needs.
and yes you would need different for ipad, iphone
First of all, your layout for iPhone will almost certainly be drastically different from iPad. For the iPads, all the different types have the same aspect ratio, so all you need to worry about is having the #2x images for the iPad with retina.
For the iPhone, however, you have the iPhone 4/4s VS the iPhone 5. Also, you have a lot less screen space, so you will need to find ways to present the same information in a smaller space. This can be done using scrollviews, tableViews, and having one part of the information in one viewController that you can switch to from another viewController.
Finally, I would highly recommend interface builder for most cases. It takes care of iPhone vs iPhone 5, and portrait vs landscape for you. It will even automatically animate the transition when rotating the phone. Sometimes, however, when you need to do custom drawing or our layout is really hard to get with interface builder, it is better to do it programmatically. In that case, a good way to check which device would be by checking the size of [[UIScreen mainScreen] applicationFrame]. Check that with the values of 1024x768 for an iPad, 320x480 for iPhone, or 320x568 for iPhone 5.
However, you really should use interface builder (for most cases). Why exactly do you want to avoid it?

iPhone 5 Optimisation

I'm super confused about how the iPhone 4 and below apps are optimised for the iPhone 5. I'm a designer. How is optimisation done? Basically I have an app with loads of knobs and buttons and other interactive components. The way I see it, there are 3 options but what is the best way (or least, standard practice)?
1) Keep the layout the same for iPhone 5 but just add extra length on the background.
2) Scale the images/layout of components from iPhone 4 to iPhone 5 so everything is proportional.
3) Have completely separate images and different layouts for both. i.e for iPhone 5, I can move components to utilise the space more. The problem with this (and I'm not a developer) is that the interaction position of the components have moved so in effect, the iPhone 4 and 5 are separate apps?
To identify if it is a iphone 5 iphone, use this code in the file nameYourApp-prefix.pch:
//Macro iPhone5
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
find the nameYourApp-prefix.pch file in the supporting files, the code must be written between:
# ifdef __ OBJC__
//Code Macro iPhone5
# endif
and then just use the "if" to check that device, like this:
if (IS_IPHONE_5) {
//Code for 4 inch screen
}else{
//Code for 3.5 inch screen
}
putting the macro in the nameYourApp-prefix.pch, all classes see it.
With a UIKit application, none of your options are ideal. You simply use Springs and Struts or Auto Layout to have your user interface elements snap to where they should be.
The solution is the same when switching between portrait and landscape orientation. You define which user interface elements can grow and which ones can't, which edges to snap to, etc.
Run it on the simulator to test the layout if you don't have an iPhone 5 device.
For example, for a UITableViewController, the UITableView should grow vertically on an iPhone 5 so that you see more rows.
I almost always have at least one control that would be nice to grow given the room. If you don't, then you'll probably just have more empty space at the bottom of the view.
How iPhone 5 Optimization Works
When you tell iOS that your app is optimized for the iPhone 5 (through use of a cleverly named image file), iOS uses your existing Springs and Struts or Auto Layout (whatever you're using) to rearrange the interface.
The first choice is the easiest and works pretty well. You'd only need to worry about having specific assets for full screen images, and make sure that NavigationBar and TabBar (in case your app have one), remains on top and bottom respectively.
Scaling other images/components such as buttons, you'll change the general aspect ratio (which you probably wouldn't want), since iPhone 4&5 display sizes only differ in height.
For developing app option two is better. Although you dont have to change all the images for iPhone 5 some images will me same like TabBarItem. But you have to create background image different for iPhone4 and 5.
Read out this apple document to create user interface.
And you can differentiate iPhone 4 from iPhone 5 using this code
UIBtton * btn = [[UIButton alloc] init];
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568)
{
btn.frame = CGRectMake(60,60, 205, 175);
//this is for iPhone5
}
else
{
btn.frame = CGRectMake(60,50, 199, 170);
//this is for iPhone4
}
Here my button place and length are different for iPhone 5 and iPhone 4
And set your control's hight and width according to iPhone Screen Size.
If you didn't, adjust your view layouts with proper auto resizing masks or look into Auto Layout if you only want to support iOS 6 going forward. You have to check height of [[UIScreen mainScreen] bounds].
Example:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568) {
// code for 4-inch screen (iPhone 5)
} else {
// code for 3.5-inch screen (< iPhone 5)
}
Note that [UIImage imageNamed:#"yourImage.png"] will only load either "background.png" or "yourImage#2x.png", it will not load "yourImage-568h#2x.png" if it exists.
Check Auto-Rotation API as Well.

Using iPhone 5 full screen for non retina game

I would like to update my non retina game to use the wide screen in the iPhone 5, is it possible to use a 568x320 sized OpenGL window?
Yes. But your graphics will not show as sharp as it could be.
I suggest you to make #2x version of all your graphics.
I have now compiled and got my game working at 568x320, all I had to do is include the Default-568h#2x file and do this in createFrameBuffer
[self setFrame:rect];
[self setBounds:rect];
[self.layer setFrame:rect];
[self.layer setBounds:rect];

converting iPhone App to iPad

I'm starting to convert iPhone App to iPad, the app is a special calculator and has a feel and look like one. on iPhone is to use portrait since it the best fit.
My questions are:
i. when I run it on the iPad do i need to use the whole iPad screen size? I would like just to upscale all my controls size and width by let's say 125% of the original size. How do I check the device screen size and how do I center my calculator to be in the center when running on iPad? currently the iPad is CGRectMake(0,0,640,1024) but it may change in the future.
ii. I also like it to adjust accordingly on both orientations of the iPad but only portrait on the iPhone. and I'm not sure how to do it in the most effective-easy way... if you may advice.
my current iPhone code has .xib with the different controls but I adjust them all using the
-setFrame:CGRectMake(...) in code, so far I set the TARGETED_DEVICE_FAMILY in project settings to run app on both devices but didn't change any of the -setFrame:CGRectMake(...) to be dynamic depending on the device.
TIA
1) You can ask [UIScreen mainScreen] for its bounds - that gives you the size.
2) You need to respond to the rotation methods in your UIViewController related to rotation:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
3) You can use this to find out if your app is running on an iPhone or iPad:
[[UIDevice currentDevice] userInterfaceIdiom]
4) To center the view in a larger view use this code:
+ (CGRect)centeredFrameForSize:(CGSize)size inRect:(CGRect)rect
{
CGRect frame;
frame.origin.x = rintf((rect.size.width - size.width)/2) + rect.origin.x;
frame.origin.y = rintf((rect.size.height - size.height)/2) + rect.origin.y;
frame.size = size;
return frame;
}

Blurry UIView with CATransform3D only on RETINA

I'm displaying a UIView with a UILabel on it and this view&label become blurry as soon as it gets to these lines code:
CATransform3D transform = CATransform3DIdentity;
transform.m34 = (1.0/-500);
view.layer.transform = transform;
Throughout the App I use CA3DRotations and other stuff and this never happened before.
Also, I set the frame of the view and the label only using integers! So it's not a half-pixel problem or something like that, I know that that causes most blurry problems, but not mine!
On the simulator it's not blurry, iPad is not blurry, iPhone3GS is not blurry. Only on an iPhone4 with Retina display it becomes blurry. Even before I do any 3D rotations! Does anybody have a clue before I go insane?
Alright, I've found a solution.
After using a hundred different lines of code using layer properties, such as layer gravity or magnification and tons of other solutions I suddenly stumbled by accident on the following 2 lines:
self.layer.shouldRasterize = TRUE;
self.layer.rasterizationScale = [[UIScreen mainScreen] scale];
This is the solution! For everyone in the future, is your view blurry on retina displays? Use this!
Have you set the contentsScale for the layer to match [UIScreen mainScreen]. scale? Try it.
It's possible your views are 'between pixels' (eg. center is [12.5, 10]). Try rounding their location and see if that helps.
If your final landing position is intended to be flat/untransformed, simply setting the transform to CATransform3D identity will also fix the problem. Depending on how things are animated, setting a final position for one of the 3D transforms to 0.0 can still introduce rounding errors and give a fuzzy appearance.