[英]How to detect IOS 7 and IOS 8 and widescreen iPhone sizes to make my app universal?
我正在為所有設備以及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 )
然后該帖子的作者建議引用-“如果您還針對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 */
}
請在這里為我提供幫助,我是一名初學者,並且希望了解使其正常工作。 我應該在哪里放置SKSpriteNode * background = [SKSpriteNode spriteNodeWithImageNamed:@“ Background”] ;?
所有這些代碼都來自Stackoverflow的其他文章,它是: 如何檢測iPhone 5(寬屏設備)?
我將圖像上傳到投遞箱,這里是鏈接https://www.dropbox.com/sh/pnll2e2jvo0uigs/AACOLbzzQqZlJEZZcBx7TMR1a?dl=0,該文件夾稱為measuredImages。 這是我用來添加背景的代碼:#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;
}
如果有人可以在宏中添加完整的代碼和用法,我將不勝感激。
重要更新:12.17.2014
如Daij-Djan所建議的,此問題通過包括正確的啟動圖像並以正確的分辨率運行我的應用程序來解決,並且我使用了屏幕邊界(與ios7中相同)。 感謝所有嘗試或幫助我解決此問題的人,我個人想感謝Daij-Djan和sha的幫助和支持。 如果您需要寬屏iphone的代碼,我將在下面的答案中保留該代碼,該代碼可在高於iPhone 4的所有iPhone和所有iPad上運行。
當我需要快速而又骯臟的方式來檢測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 ))
使用這非常有用
#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)
要檢測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);
只需包含正確的啟動圖像,您的應用將以正確的分辨率運行,並且您可以使用屏幕邊界[與ios7中相同]
我再次強調:包括正確的啟動圖像! 然后使用UIScreen bounds
好的,對這個問題的補救是將正確的啟動圖像包括到image.xcassets或LaunchImage.xib中,編譯器將為您的圖像選擇正確的大小屏幕,如上面答案中的Daij-Djan所述。 為了使其適用於寬屏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 )
並將此代碼用於檢測寬屏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,寬屏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。 您可以從Dropbox下載針對特定屏幕尺寸優化的圖像。 這是鏈接https://www.dropbox.com/sh/pnll2e2jvo0uigs/AACOLbzzQqZlJEZZcBx7TMR1a?dl=0並嘗試一下。 最后也是最重要的事情是為每個屏幕尺寸添加啟動圖像,否則代碼將無法正常工作。 我希望這對您有所幫助,並感謝你們教給我的所有信息,我因為信息錯誤而失去了2個月才能正常工作。
它適用於5c&6&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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.