如何检测IOS 7和IOS 8以及宽屏iPhone尺寸以使我的应用程序具有通用性?

时间:2014-12-16 01:36:43

标签: ios objective-c iphone macros

我正在为所有设备和IOS 7和IOS 8开发通用的IOS应用程序。我有这个宏:

此宏用于检测宽屏iPhone 5,适用于IOS 7:

#define IS_WIDESCREEN_IOS7 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

此宏也适用于宽屏iPone 5,但仅适用于IOS 8:

#define IS_WIDESCREEN_IOS8 ( fabs( ( double )[ [ UIScreen mainScreen ] nativeBounds ].size.height - ( double )1136 ) < DBL_EPSILON )

我需要结合使用此代码使其在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 )

然后该帖子的作者建议引用 - &#34;如果您还定位iOS 7或更低版​​本,请务必使用功能检测,因为在iOS 8之前调用nativeBounds会导致您的应用崩溃:& #34;并提供以下代码:

if( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] )
{
    /* Detect using nativeBounds - iOS 8 and greater */
}
else
{
    /* Detect using bounds - iOS 7 and lower */
}

请在这里帮助我,我是一名初学者,希望了解如何使其发挥作用。我应该在哪里放SKSpriteNode * background = [SKSpriteNode spriteNodeWithImageNamed:@&#34; Background&#34;];?

所有这些代码都来自Stackoverflow帖子中的不同帖子:How to detect iPhone 5 (widescreen devices)?

我上传图片到drop框这里是链接https://www.dropbox.com/sh/pnll2e2jvo0uigs/AACOLbzzQqZlJEZZcBx7TMR1a?dl=0文件夹名为measuredImages。这是我用来添加背景的代码:#import&#34; GameScene.h&#34;

@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;
}

如果有人可以将完整的代码与宏和用法放在一起,我会非常感激。

重要 更新:12.17.2014

这个问题通过包含正确的启动图像和我的应用程序以正确的分辨率运行来解决,我使用屏幕边界[与ios7相同],正如Daij-Djan建议的那样。感谢所有试过或帮助我解决这个问题的人,我个人要感谢Daij-Djan和sha的帮助和支持。如果您需要宽屏iphone的代码,我将在下面的答案中留下它,它可以在iPhone 4和所有iPad上面的所有iPhone上运行。

6 个答案:

答案 0 :(得分:2)

当我需要快速而肮脏的方式来检测iOS7 / 8和iPhone / iPad设备时,我将使用以下宏:

#define IS_IOS8     ([[UIDevice currentDevice].systemVersion compare:@"8.0" options:NSNumericSearch] != NSOrderedAscending)
#define IS_IPHONE   ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)

您可以使用这些宏:

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
   ...
}

更新:要检测宽屏幕设备,您可以使用以下宏(因为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 ))

答案 1 :(得分:2)

使用这非常有用

#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)

答案 2 :(得分:1)

要检测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)

示例:

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
    // code here
}

要检测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);

答案 3 :(得分:1)

你不需要专门检测屏幕宽度。

只包含正确的启动图像,您的应用程序将以正确的分辨率运行,您只需使用屏幕边界[与ios7相同]

我再次强调:包括正确的发布图像! 然后使用UIScreen bounds

https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/LaunchImages.html

答案 4 :(得分:1)

好的,这个问题的解决方法是将正确的启动图像包含到image.xcassets或LaunchImage.xib中,编译器将为你的图像挑选正确大小的屏幕,就像上面的答案中提到的Daij-Djan一样。使其适用于宽屏iPhone和所有iPhone(4,4及以上)以及iPad和IOS 7以及IOS 8.将此宏添加到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 )

并使用此代码检测宽屏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;
}

最后一步是用这种方式命名的名字:适用于iPhone 4,4s的Background@2x.png, Background-568 @ 2x适用于宽屏iPhone 5,5c,5s,适用于宽屏iPhone 6的Background-667 @ 2x.png,适用于iPhone 6 Plus的background@3x.png,适用于iPad常规显示的Background~iPad.png以及最后背景~iPad适用于iPad Retina Display的@ 2x.png。 您可以从Dropbox下载针对特定屏幕尺寸优化的此图像。这是链接https://www.dropbox.com/sh/pnll2e2jvo0uigs/AACOLbzzQqZlJEZZcBx7TMR1a?dl=0并尝试一下。最后也是最重要的是为每个屏幕尺寸添加启动图像,否则代码将无法正常工作。我希望这有帮助,感谢那些教我这一切的人,我失去了2个月才能让它工作,因为我有错误的信息。

答案 5 :(得分:1)

适用于5c&amp; 6&amp; 6plus。 它将检查屏幕是否为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;

}
相关问题