0

I am developing a game using SpriteKit. How do I specify a different 2x graphics for the iphone5 and iPhone6 since they both use the @2x slot in the asset catalog folder?

I have tried changing the scale mode and it doesn't seem to have any effect.

JSON C11
  • 11,272
  • 7
  • 78
  • 65
user6025921
  • 165
  • 1
  • 5
  • http://stackoverflow.com/a/29356711/3108877 – Rob Oct 02 '16 at 16:18
  • It does not make sense to use 2 different images for `@2x` graphics, the point of it is to double the given pixels of the original graphics (Which as of iOS 10 is no longer used unless scaling by .5x.5 picks this, will need to test) Think about it, you have a 16x16 image, `@2x` is a 32x32 image, having 2 images at 32x32 does not make sense. How about you explain what the real problem is, and we can come up with a valid solution to meet your needs – Knight0fDragon Oct 03 '16 at 05:58
  • Thanks everyone for the solutions! I apologize if my question wasn't clear. I needed to know how to implement scalemode so that my game would scale from iphone 6+ all the way down to iphone 5. – user6025921 Oct 03 '16 at 20:28

1 Answers1

0

This solution work for Swift 2.x (below you can find the Swift 3.x code)

You could do:

struct ScreenSize
{
    static let SCREEN_WIDTH         = UIScreen.mainScreen().bounds.size.width
    static let SCREEN_HEIGHT        = UIScreen.mainScreen().bounds.size.height
    static let SCREEN_MAX_LENGTH    = max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
    static let SCREEN_MIN_LENGTH    = min(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
}

struct DeviceType
{
    static let IS_IPHONE            = UIDevice.currentDevice().userInterfaceIdiom == .Phone
    static let IS_IPHONE_4_OR_LESS  = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0
    static let IS_IPHONE_5          = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0
    static let IS_IPHONE_6          = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0
    static let IS_IPHONE_6P         = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0
    static let IS_IPAD              = UIDevice.currentDevice().userInterfaceIdiom == .Pad && ScreenSize.SCREEN_MAX_LENGTH == 1024.0
    static let IS_IPAD_PRO          = UIDevice.currentDevice().userInterfaceIdiom == .Pad && ScreenSize.SCREEN_MAX_LENGTH == 1366.0
}

Swift 3

You could do:

struct ScreenSize
{
    static let SCREEN_WIDTH         = UIScreen.main.bounds.size.width
    static let SCREEN_HEIGHT        = UIScreen.main.bounds.size.height
    static let SCREEN_MAX_LENGTH    = max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
    static let SCREEN_MIN_LENGTH    = min(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
}

struct DeviceType
{
    static let IS_IPHONE            = UIDevice.current.userInterfaceIdiom == .phone
    static let IS_IPHONE_4_OR_LESS  = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0
    static let IS_IPHONE_5          = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0
    static let IS_IPHONE_6          = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0
    static let IS_IPHONE_6P         = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0
    static let IS_IPAD              = UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.SCREEN_MAX_LENGTH == 1024.0
    static let IS_IPAD_PRO          = UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.SCREEN_MAX_LENGTH == 1366.0
}

Usage (equal for both Swift 2.x and Swift 3):

if DeviceType.IS_IPHONE_5 {
   // do stuff for my iPhone 5
}
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
  • Thank you. Maybe I was thinking this is another question on scaleMode, but it's not specified clear, so probably in my opinion a way to walk could be this kind of answer. We will hear the comment of OP to know more information about is issue – Alessandro Ornano Oct 03 '16 at 06:47
  • Thanks Alessandro! This is a great solution, but I was primarily looking for a solution to implement scalemode so that my game would scale from iphone6+ down to iphone5. – user6025921 Oct 03 '16 at 20:30