简体   繁体   中英

iOS convert coordinates from iPhone to iPad compatibility

When I first started programming for iOS (with Cocos2d) I used a bunch of hardcoded coordinates based on 320x480 screen size for the iPhone. I just realized that I need to include at least a basic version of iPad and the iPad mini.

I have read these:

cocos2d (but i'm not sure if this is the best way?) cocos2d:Convert iPhone App to Universal App

converting coordinates: Convert coordinates from iPhone screen size to iPad screen size

converting iphone to ipad: http://iphonedevelopment.blogspot.com/2010/04/converting-iphone-apps-to-universal.html

Apple's documentation on creating universal app: http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AdvancedAppTricks/AdvancedAppTricks.html#//apple_ref/doc/uid/TP40007072-CH7-SW24

  1. is there an easy way to convert the graphical display from iPhone to iPad without editing every part of my code where I used a hard coded value? (50 spots, all in 1 file)
  2. what's the best practice for future coding with iOS/Cocos2D coordinates? should I do

    #define SPACE_SHIP_X_IPAD 384

    #define SPACE_SHIP_X_IPHONE 160

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){

     `[ship.setPosition ccp:(SPACE_SHIP_X_IPHONE,0)];` 

    } else {

     `[ship.setPosition ccp:(SPACE_SHIP_X_IPAD,0)];` 

    }

    (by the way, will someone who knows how to format this code with the "#" sign, plz teach me how to format the above code?)

  3. is it recommended that I create 2 different apps - one for iphone and one for iPad? or this universal approach of only 1 app, and then selecting the distribution type when I submit to Appstore?

  4. are Mac versions of apps the same distribution channel as the app store for iPhone and iPad? what are the advantages or disadvantages to distributing on Mac as well? (i'm guessing it's not good because graphics would have to accomodate something liek the massive iMac?)
  5. just to make sure - my app file (which doesn't use anything iphone specific like calling) will work on the iPod and iPad Mini without ANY extra work, right?

Any help would be greatly appreciated. I will upvote all good attempts

It looks like your approach is overly complicated. Making a universal application typically involves never hard-coding position values, but rather calculating these position values based on the bounds of the device.

For example:

//Button in center for universal
CGPoint buttonOffset = CGPointMake( ([[UIScreen mainScreen] bounds].size.width - button.frame.size.width) / 2, ([[UIScreen mainScreen] bounds].size.height - button.frame.size.height) / 2);

button.frame = CGRectMake(buttonOffset.x, buttonOffset.y, button.frame.size.width, button.frame.size.height);

Use similar concepts to create buttons (or ships) with positions that are relative to the screen bounds. This will save you a lot of time and grief.

One thing to note is that the "bounds" of the screen will not factor in the device orientation. If you want to support multiple orientations you will find it useful to create a utility method to handle that logic for you.

Something like:

+ (CGSize) deviceSize {
    CGSize size;
    if(UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])){
        size = CGSizeMake([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);
    } else {
        size = CGSizeMake([[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);        
    }
    return size;
}

I know you want an easy solution so you don't have to change all your values but I would recommend biting the bullet and changing the values. It will help you a lot in the long-run. Not only for this application but the practice will help in any application you create in the future.

Is it recommended that you create two different apps for universal? No, it is not recommended. You can select "universal" when creating the xcode project and also change it in an existing project. You will not need to create two builds or binaries either. Apple will recognize it as universal and make it available in the store for all devices. However, you will need to create multiple versions of a splash screen to accommodate different screen sizes and pixel densities.

are Mac versions of apps the same distribution channel as the app store for iPhone and iPad? Sort of... you can deploy to the app store and you gain more freedom since you can package and distribute the OSX binary yourself -- but you will have to create a separate app since you will most likely be using CocoaTouch libraries which are not supported on the mac. You will want to preserve functionality that does not use CocoaTouch such as your Rendering and Game Logic. If you are deploying to OSX, the large screen sizes will be the least of your concern. Advantages: Wider Audience. Disadvantages: Requires a lot of planning when architecting your app/game so you can reuse as much code as possible and cater to all screens.

just to make sure - my app file (which doesn't use anything iphone specific like calling) will work on the iPod and iPad Mini without ANY extra work, right? Well... some extra work With some additional image assets for home screen icons and a splash screen you will be good to go.

I do this with data files shared through iCloud so that data appears in the right place on both devices... X *= 768 / 320 and Y *= 1024 / 480. To go back to iPhone from iPad resolution, just divide instead of multiply. It's simple and precise. Then if you need to, record which set of coordinates your data is configured for.

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