writing cocos2d code for both iphone 4s and iphone 5 - ios

I know cocos2d can take into consideration whether code is run on iphone or ipad using
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
}
But can I write cocos2d code takes into consideration both sizes of the iphone screen. For Example:
#define xPosition1 120.0
#define xPosition2 240.0
#define xPosition3 360.0
if (iphone5) {
#define xPosition1 142.0
#define xPosition2 284.0
#define xPosition3 426.0
}

One approach is to use the following:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
This will return you the screen width and height depending on your device. That means, the iPhone 4S will return 320 x 480 and iPhone 5 will return 320 x 568

Related

Why Xcode detects my iPhone 7 plus device as iPhone 5 with wrong screen scale and size?

I run my app on my iPhone 7 + device and when I try to log the Scale and screen bound sizes, What I get is related to iPhone 5/5s/5c/SE, NOT to my iphone 7 plus specification.
This is what I get in log:
CGFloaf scale = [[UIScreen mainScreen] scale];
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
NSLog (#"Scale: %f", scale);
NSLog (#"screenHeight: %f", screenHeight);
Output :
Scale: 2.000000
screenHeight: 568.000000
Why is that??! I appreciate any help.

ios using size classes

Using ios Size classes how do I differentiate between iphone 5.5inch in portrait and any other iphone in portrait.
For example if I set the text size - width compact and height any - this seems to apply to ALL iphones. Is there a size class that you can apply to iphone 6 - that is different from all the other smaller iphones?
If I understood your problem correctly, you have to do this programmatically.
Use this code to check for the size of the iPhone:
if ([[UIScreen mainScreen] bounds].size.height == 480) {
//3.5 inch iPhone
}
if ([[UIScreen mainScreen] bounds].size.height == 568) {
//4 inch iPhone
}
if ([[UIScreen mainScreen] bounds].size.height == 667) {
//4.7 inch iPhone
}
if ([[UIScreen mainScreen] bounds].size.height == 736) {
//5.5 inch iPhone
}

How to detect Retina HD display?

UIScreen has a new, nativeScale property in iOS 8 but the documentation does not say a word about it.
#property(nonatomic, readonly) CGFloat nativeScale
There is also a scale property but the docs say it is 2 for retina displays.
#property(nonatomic, readonly) CGFloat scale
I am wondering if there is a way to distinguish the displays. The reason why I need to know whether the device has Retina HD display is because I want to request images with different sizes based on the displays.
Thanks for any help!
Below works very well to detect type of display on iPhone6Plus.
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)] && [[UIScreen mainScreen] scale] == 3.0)
NSLog(#"Retina HD");
else
NSLog(#"Non Retina HD");
Check the below code:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGFloat screenScale = [[UIScreen mainScreen] scale];
CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);
NSLog(#"WIDTH:%f",screenSize.height);
NSLog(#"WIDTH:%f",screenSize.width);
float height =screenSize.height;
float width = screenSize.width;
NSString *deviceType=#"";
if (height==1136.000000 && width==640.000000)
{
deviceType =#"iPhone 5,5S and 5C Retina";
}
if (height==1920.000000 && width==1080.000000)
{
deviceType =#"iPhone 6 Plus Retina Downsample";
}
if (height==2208.000000 && width==1242.000000)
{
deviceType =#"iPhone 6 Plus Retina";
}
if (height==1334.000000 && width==750.000000)
{
deviceType =#"iPhone 6 Retina";
}
if (height==960.000000 && width==640.000000)
{
deviceType =#"iPhone 4 and 4S Retina";
}
if (height==480.000000 && width==320.000000)
{
deviceType =#"iPhone 3g and 3gs Non retina ";
}
NSLog(#"Your Result:%#",deviceType);
Reference:
iPhone Resoution

Size of scene for iphone 3.5-inch and 4-inch in cocos2d v3.0

I have some trouble with size of scene.I get always same size UIScreen, therefore I can't determine screen size, I always get "320x480" at 3.5-inch iphone & 4-inch iphone. How can I start getting screen sizes instead of "320x480"? Thanks in advance http://lvkr.ru/u49S4A.jpg
Have you tried
[[UIScreen mainScreen] bounds].size
?
Examining the size's height and width should return 320x480 for a 3.5 inch screen, and 320x568 for a 4 inch screen.
use this for checking whether the device is iPhone 4 or 3.5 inch,then get y position or height for controller or view
+(BOOL)isDeviceiPhone5
{
BOOL iPhone5 = FALSE;
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568) {
// code for 4-inch screen
iPhone5 = TRUE;
}
else
{
iPhone5 = FALSE;
// code for 3.5-inch screen
}
return iPhone5;
}
+(CGRect )getFrameFor_iPhone4_height:(CGRect)frame
{
frame.size.height = frame.size.height - 87.0;
return frame;
}
+(CGRect )getFrameFor_iPhone4_yPos:(CGRect)frame
{
frame.origin.y = frame.origin.y - 87.0;
return frame;
}

Detect iPhone different resolution in iOS

I am using cocos2d in iOS .
I am having a problem detecting the iphone5 resolution(1136*640).
I am using code:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
When we run app on iPhone simulator (4 inch), it gives screenWidth and screnHeight-320,480 respectively.
Can anyone tell me the solution for this?
Write this code in .m file.
#define WIDESCREEN ( fabs( ( double )[ [UIScreen mainScreen ]bounds ].size.height-( double )568 ) < DBL_EPSILON )
Now you can know the screen size like this.
if(WIDESCREEN)
{
NSLog(#"iPhone5 is here");
}
else
{
NSLog(#"iPhone4 is here");
}
you can add checks in your code for iPhone 4inch screens and 3.5 inch screens
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
// iPhone Classic
// set your frames for classic iPhone here
}
if(result.height == 568)
{
// iPhone 5, iPhone 5S, iPhone 5C
// set your frames for 4 inch iPhone here
}
iPhone 5 resolution is 1136*640 only. If you wanna check just Log below Code
the resolution it will give exactly (1136*640)
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGFloat screenScale = [[UIScreen mainScreen] scale];
CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);
NSLog(#"Screen SIze %#",NSStringFromCGSize (screenSize));
#define IS_IPHONE5 ([[UIScreen mainScreen] bounds].size.width >= 568 || [[UIScreen mainScreen] bounds].size.height >= 568)?YES:NO
#define IS_IPHONE4 ([[UIScreen mainScreen] bounds].size.width >= 480 || [[UIScreen mainScreen] bounds].size.height >= 480)?YES:NO
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
if(IS_IPHONE4)
{
// iPhone 4
}
if(IS_IPHONE5)
{
// iPhone 5
}
}

Resources