Search code examples
swiftsprite-kitframecgrect

CGRectGet doesn't work well


I'm working in Swift with XCode 6 and Sprite Kit and I want to have the biggest pixel in y, so i'm using this function :

CGRectGetMaxY(frame)

and I tested my code with the iPhone 6 simulator, so the value should be 1334.0 but the console shows me only 667.0 ...

What is wrong ?


Solution

  • It appears you're working in "point-space" and not "pixel-space". Depending on your settings, your image (I'm assuming that's where 1334 comes from) is likely at a @2x resolution.

    1. Check your assets folder to make sure the image is defined how it should be.
    2. Check your scale settings
    3. UIKit (and most of iOS) runs on point-space, so try to work in these coordinates - they abstract device resolution (important now that we have @1x, @2x, and @3x devices).

    Point-space:

    The original iPhone was 320x480 pixels. In order to have your code be device-agnostic, when they went to Retina, the screen and all the logic stayed at a resolution of 320x480. These didn't always correspond to pixels, so the terminology changes to "points". That's why when you grab the screen size for an iPhone 4 it still reports 320x480.

    Apple's Documentation on this.