[英]How to detect IOS 7 and IOS 8 and widescreen iPhone sizes to make my app universal?
I am developing universal IOS app for all devices and IOS 7 and IOS 8. and I have this macros: 我正在为所有设备以及IOS 7和IOS 8开发通用的IOS应用程序。我有以下宏:
This macros is for detecting widescreen iPhone 5, this works for IOS 7: 此宏用于检测宽屏iPhone 5,适用于IOS 7:
#define IS_WIDESCREEN_IOS7 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
This macros is also for for widescreen iPone 5, but works only for IOS 8: 此宏也适用于宽屏iPone 5,但仅适用于IOS 8:
#define IS_WIDESCREEN_IOS8 ( fabs( ( double )[ [ UIScreen mainScreen ] nativeBounds ].size.height - ( double )1136 ) < DBL_EPSILON )
I need to combine this code to make it work on both IOS 7 and IOS 8and for that I need selector that detects IOS version., here is the code: 我需要组合此代码以使其在IOS 7和IOS 8上都能工作,为此,我需要选择器来检测IOS版本。这是代码:
#define IS_WIDESCREEN_IOS7 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
#define IS_WIDESCREEN_IOS8 ( fabs( ( double )[ [ UIScreen mainScreen ] nativeBounds ].size.height - ( double )1136 ) < DBL_EPSILON )
#define IS_WIDESCREEN ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_WIDESCREEN_IOS8 : IS_WIDESCREEN_IOS7 )
then the author of the post suggests to quote -"If you're also targeting iOS 7 or lower, be sure to use feature detection, as calling nativeBounds prior to iOS 8 will crash your app:" and gives following code: 然后该帖子的作者建议引用-“如果您还针对iOS 7或更低版本,请确保使用功能检测,因为在iOS 8之前调用nativeBounds会使您的应用程序崩溃:”并给出以下代码:
if( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] )
{
/* Detect using nativeBounds - iOS 8 and greater */
}
else
{
/* Detect using bounds - iOS 7 and lower */
}
Please help me here I am a beginner developer and want to understand to to make it work. 请在这里为我提供帮助,我是一名初学者,并且希望了解使其正常工作。 Where should I put SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"Background"];? 我应该在哪里放置SKSpriteNode * background = [SKSpriteNode spriteNodeWithImageNamed:@“ Background”] ;?
All this code is from different post in Stackoverflow post here is it: How to detect iPhone 5 (widescreen devices)? 所有这些代码都来自Stackoverflow的其他文章,它是: 如何检测iPhone 5(宽屏设备)?
I uploaded images to drop box here is the link https://www.dropbox.com/sh/pnll2e2jvo0uigs/AACOLbzzQqZlJEZZcBx7TMR1a?dl=0 the folder is called measuredImages. 我将图像上传到投递箱,这里是链接https://www.dropbox.com/sh/pnll2e2jvo0uigs/AACOLbzzQqZlJEZZcBx7TMR1a?dl=0,该文件夹称为measuredImages。 here is the code I use for adding background: #import "GameScene.h" 这是我用来添加背景的代码:#import“ GameScene.h”
@implementation GameScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"Background-568"]; background.anchorPoint = CGPointMake(0.5, 1);
background.position = CGPointMake(self.size.width/2, self.size.height);
[self addChild:background];}
return self;
}
If someone could put full code with macros and usage in answer I would greatly appreciate it. 如果有人可以在宏中添加完整的代码和用法,我将不胜感激。
Important UPDATE:12.17.2014 重要更新:12.17.2014
This problem was solved by including the right launch images and my app run in the right resolution and I used screen bounds [same as in ios7], as Daij-Djan suggested. 如Daij-Djan所建议的,此问题通过包括正确的启动图像并以正确的分辨率运行我的应用程序来解决,并且我使用了屏幕边界(与ios7中相同)。 Thanks to everybody who tried or helped me to solve this problem,I personally want to thank Daij-Djan and sha for help and support. 感谢所有尝试或帮助我解决此问题的人,我个人想感谢Daij-Djan和sha的帮助和支持。 If you need the code for widescreen iphones I will leave it in my own answer below, it runs on all iPhones above iPhone 4 and all iPads. 如果您需要宽屏iphone的代码,我将在下面的答案中保留该代码,该代码可在高于iPhone 4的所有iPhone和所有iPad上运行。
When I need a quick and dirty way detecting iOS7/8 and iPhone/iPad device I will use the following macros: 当我需要快速而又肮脏的方式来检测iOS7 / 8和iPhone / iPad设备时,我将使用以下宏:
#define IS_IOS8 ([[UIDevice currentDevice].systemVersion compare:@"8.0" options:NSNumericSearch] != NSOrderedAscending)
#define IS_IPHONE ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
And you can use these macros: 您可以使用以下宏:
if (IS_IPHONE) {
// iPhone specific code
...
}
else {
// iPad specific code
...
}
if (IS_IOS8) {
// Code specific to iOS8+
...
}
else {
// Code specific to earlier versions of iOS
...
}
UPDATE: To detect wide screen devices you can use the following macro (since iOS8 UIScreen will be orientation aware and height will be different in portrait/landscape, so you can check for both: 更新:要检测宽屏设备,您可以使用以下宏(因为iOS8 UIScreen可以识别方向,并且纵向/横向的高度不同,因此可以同时检查两者:
#define IS_WIDESCREEN (( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON ) || ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.width - ( double )568 ) < DBL_EPSILON ))
Use This is Very useful 使用这非常有用
#define IS_IPHONE ((int)(MAX([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)) == 480)
#define IS_IPHONE5 ((int)(MAX([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)) == 568)
#define IS_IPHONE6 ((int)(MAX([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)) == 667)
#define IS_IPHONE6PLUS ((int)(MAX([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)) == 736)
To detect iOS versions you can use one of the following macros : 要检测iOS版本,可以使用以下宏之一:
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
Example: 例:
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
// code here
}
And to detect iPhone types, you can use, 要检测iPhone类型,您可以使用
CGSize applicationFrameSize = [UIScreen mainScreen].bounds.size;
CGFloat maxHeight = (MAX(applicationFrameSize.width, applicationFrameSize.height));
_is4GDevice = (maxHeight == 480.0 || maxHeight == 480.0);
_is5GDevice = (maxHeight == 568.0 || maxHeight == 568.0);
_is6GDevice = (maxHeight == 667.0 || maxHeight == 667.0);
_is6PlusDevice = (maxHeight == 736.0 || maxHeight == 736.0);
just include the right launch images and your app will run in the right resolution and you can just use the screen bounds [same as in ios7] 只需包含正确的启动图像,您的应用将以正确的分辨率运行,并且您可以使用屏幕边界[与ios7中相同]
I stress again: include right launch images! 我再次强调:包括正确的启动图像! THEN use the UIScreen bounds
然后使用UIScreen bounds
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/LaunchImages.html https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/LaunchImages.html
Ok so the salvation to this problem is to include right launch images to image.xcassets or LaunchImage.xib and compiler will pick right size screen for your images as Daij-Djan mentioned in above answer. 好的,对这个问题的补救是将正确的启动图像包括到image.xcassets或LaunchImage.xib中,编译器将为您的图像选择正确的大小屏幕,如上面答案中的Daij-Djan所述。 To make it work for widescreen iPhones and all iPhones (4,4s and above) and iPads and IOS 7 and IOS 8. add this macros your MyScene.m file or just any .m file where you use it. 为了使其适用于宽屏iPhone和所有iPhone(4,4s及更高版本)以及iPad和IOS 7和IOS8。将此宏添加到MyScene.m文件或使用它的任何.m文件中。
#define IS_WIDESCREEN_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
#define IS_WIDESCREEN_6 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )667 ) < DBL_EPSILON )
and use this code for detecting widescreen iPhones, this works for all IOS devices and IOS 7 and IOS 8: 并将此代码用于检测宽屏iPhone,该代码适用于所有IOS设备以及IOS 7和IOS 8:
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
SKSpriteNode *background;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
if (IS_WIDESCREEN_5) {
//detects WIDESCREEN iPhone 5,5c,5s for both IOS 7,8
background= [SKSpriteNode spriteNodeWithImageNamed:@"Background-568"];
}else if (IS_WIDESCREEN_6){
//detects WIDESCREEN iPhone 6 for both IOS 7,8
background= [SKSpriteNode spriteNodeWithImageNamed:@"Background-667"];
}else{
//detects iPhone 4,4s,iPhone 6 Plus, for both IOS 7,8
background= [SKSpriteNode spriteNodeWithImageNamed:@"Background"];
}
}else{
//detects iPads all sizes and resolutions (Ipad regular display and iPad retina display)
background= [SKSpriteNode spriteNodeWithImageNamed:@"Background~iPad"];
}
background.anchorPoint = CGPointMake(0.5, 1);
background.position = CGPointMake(self.size.width/2, self.size.height);
[self addChild:background];
}
return self;
}
And last step is name you images this way: Background@2x.png for iPhone 4,4s, Background-568@2x for widescreen iPhone 5,5c,5s, Background-667@2x.png for widescreen iPhone 6, background@3x.png for iPhone 6 Plus, Background~iPad.png for iPad regular display and lastly Background~iPad@2x.png for iPad Retina Display. 最后一步就是用这种方式命名您的图像:iPhone 4,4s的Background@2x.png,宽屏iPhone 5,5c,5s的Background-568 @ 2x,宽屏iPhone 6的Background-667 @ 2x.png,background @ 3x iPhone 6 Plus为.png,iPad常规显示为Background〜iPad.png,iPad Retina Display为Background~iPad@2x.png。 You can download this images optimized for specific screen sizes from dropbox. 您可以从Dropbox下载针对特定屏幕尺寸优化的图像。 Here is the link https://www.dropbox.com/sh/pnll2e2jvo0uigs/AACOLbzzQqZlJEZZcBx7TMR1a?dl=0 and try it. 这是链接https://www.dropbox.com/sh/pnll2e2jvo0uigs/AACOLbzzQqZlJEZZcBx7TMR1a?dl=0并尝试一下。 Last and most important thing is to add launch Images for each screen size otherwise the code will not work. 最后也是最重要的事情是为每个屏幕尺寸添加启动图像,否则代码将无法正常工作。 I hope this helps and thanks for you guys who teach me all this I lost 2 months to get it work because I had wrong info. 我希望这对您有所帮助,并感谢你们教给我的所有信息,我因为信息错误而失去了2个月才能正常工作。
It works on 5c & 6 & 6plus. 它适用于5c&6&6plus。 It will check whether the screen is 16 : 9. I am a new iOS programmer, please advise 它将检查屏幕是否为16:9。我是新的iOS程序员,请告知
bool IsTargetDeviceWideScreen()
{
double screenWidth = 0;
double screenHeight = 0;
if ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) {
CGSize screenSize = [ [ UIScreen mainScreen ] nativeBounds ].size;
screenWidth = screenSize.width;
screenHeight = screenSize.height;
}
else
{
CGSize screenSize = [ [ UIScreen mainScreen ] bounds ].size;
screenWidth = screenSize.width;
screenHeight = screenSize.height;
}
NSLog(@"screen size");
NSLog(@"%f", screenWidth);
NSLog(@"%f", screenHeight);
double rateWidthHeight = 0;
if (screenWidth < screenHeight) {
rateWidthHeight = (screenWidth * 16) / (screenHeight * 9);
}
else
{
rateWidthHeight = (screenWidth * 9) / (screenHeight * 16);
}
NSLog(@"%f", rateWidthHeight);
if ( 0.99 < rateWidthHeight & rateWidthHeight < 1.01) {
return true;
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.