简体   繁体   中英

How do I implement macros for IOS 7?

I trying to make universal app for all IOS devises except for iPhone below 4 and I have this macros that work for IOS 8:

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

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

here is the code I am using:

-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
   SKSpriteNode *background;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {

    if (IS_IPHONE_6) {
        background = [SKSpriteNode spriteNodeWithImageNamed:@"Background-667"];
    }
   else if (IS_IPHONE_5) {
        background = [SKSpriteNode spriteNodeWithImageNamed:@"Background-568"];
    }else{
        background = [SKSpriteNode spriteNodeWithImageNamed:@"Background"];
    }
    }else{
        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;
}

but these macros doesn't work for IOS 7 to detect optimized images. In this forum How to detect iPhone 5 (widescreen devices)? they say you need to put this code and it will work

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

but when I do the app crashes.

What do I need to add to make it work? I am a beginner developer and dont understand how this macros work.

The macro IS_WIDESCREEN is written correctly because it asks the system if it can responds to the message -nativeBounds .
For IS_IPHONE_6 and 5 you should write something like the widescreen macro, -nativeBounds was only implemented in iOS8 that's why it crashes if you launch your app on iOS7.
Probably using images asset in Xcode you can achieve what you want by simply creating an images asset that load different image for different screen size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM