TerranceW
TerranceW

Reputation: 286

Scaling for specific devices

I am running into some issues trying to make my SpriteKit game compatible on all devices. I understand how Image.xcassets works, as well as how to reference the phone frame in placing my nodes.

For example, my game looks fine on 6, 6+ and the iPads. Unfortunately, it has the top the cut off for the earlier iPhones. An easy fix for this would just be to reference the top of the screen like so:

node.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame) - 5)

This works for all the phones, but not the iPads

However if I reference the top of the device like that, then on the iPad the upper nodes are too far up the screen such that the hero is not able to interact with them.

The current way my code is written for positioning of nodes is just by using other nodes to build on top of:

node.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinY(self.frame) + node2.size.height + node3.size.height)

This works for the 6, 6+ and the iPads

How can I make it so there is compatibility on all devices? I Know I could adjust the "2x" image so it could fit for the 5 and 5s, but then it would throw off the game mechanic for 6 (because the 6 and 5 series share the "2x" for some reason).

Is there a way I can specifically test for use on a certain device, and then have certain code run in that case? Are there any solutions? Thanks.

Upvotes: 1

Views: 71

Answers (2)

Larry Pickles
Larry Pickles

Reputation: 4646

this is even easier use a ternary operator operator in Swift:

This checks for sizes of height greater than the iphone 6+ which would mean its' an iPad, then set a custom sized image accordingly, you can even use Lucas' macros he posted above:

asdf!.qrImageView.image = UIScreen.mainScreen().bounds.height  > 736 ? UIImage(named: "check") : UIImage(named: "bubbles")

Heres' the full code, try this now, obviously change the image to your needs, the call above is using very customized UIView subclass, this next call is not, its using a UIImageView:

override func viewDidLoad() {

    super.viewDidLoad()

    var floatiesW = 200 as CGFloat
    var floatiesH = 200 as CGFloat

    var asdf = UIImageView()
    asdf.backgroundColor = UIColor.redColor()
    asdf.frame = CGRectMake((self.view.bounds.width/2.0) - (floatiesW/2.0), (self.view.bounds.height/2.0) - (floatiesH/2.0), floatiesW, floatiesH)
    asdf.image = self.view.bounds.height  > 736 ? UIImage(named: "check") : UIImage(named: "bubbles")
    self.view.addSubview(asdf)
}

and this:

UIScreen.mainScreen().bounds.height  > 736 ? UIImage(named: "check") : UIImage(named: "bubbles")

in most cases can be swapped with this:

self.view.bounds.height  > 736 ? UIImage(named: "check") : UIImage(named: "bubbles")

but be careful, you should probably use the UIScreen.main .. one

Upvotes: 3

Lucas Azzopardi
Lucas Azzopardi

Reputation: 190

Put this code right below the imports to check to see what kind of device the users are running.

enum UIUserInterfaceIdiom1 : Int
{
case Unspecified
case Phone
case Pad
}

struct ScreenSize1
{
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 DeviceType1
{
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
}

Then in the viewDidLoad do a simple if statement in order to see what device is being used and then run the necessary code under each one.

if UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0 {
        println("Iphone 4s")
        // put code here
    }

And then do this for each device. It is a little tedious obviously but it should do the trick. Also above I do not have the iPad specifications listed but you can easily provide those just by figuring out the screen height of them. Hope this helps!

Upvotes: 2

Related Questions