如何使用iOS 8 SDK重新调整PlayN应用程序的大小?

时间:2014-09-05 18:33:47

标签: xamarin ios8 playn

此问题的上下文是iOS 8,Xamarin和PlayN,仅在横向模式下应用。

我曾经基于FinishedLaunchingUIScreen.MainScreen.Bounds重新调整我的应用程序以适应不同的屏幕尺寸。使用iOS 8 SDK时,API的语义发生了变化。以前,UIScreen不依赖于方向。以前用于以前工作正常的SDK的代码:

public partial class AppDelegate : UIApplicationDelegate
{
    public override bool FinishedLaunching (UIApplication app, NSDictionary options)
    {
        app.SetStatusBarStyle (UIStatusBarStyle.LightContent, false);

        IOSPlatform.Config config = new IOSPlatform.Config ();
        config.orients = IOSPlatform.SupportedOrients.LANDSCAPES;
        IOSPlatform.register (app, config);

        System.Drawing.RectangleF bounds = UIScreen.MainScreen.Bounds;

        // Flip width and height when in landscape for iOS SDK before iOS 8
        float screenHeight = bounds.Width; 
        float screenWidth  = bounds.Height;

        float heightRatio = screenHeight / MyGame.HEIGHT;
        float widthRatio  = screenWidth / MyGame.WIDTH;

        float ratio = (heightRatio < widthRatio ? heightRatio : widthRatio);
        PlayN.graphics ().rootLayer ().setScale (ratio, ratio);
        MyGame game = new MyGame ();
        PlayN.run (game);
        return true;
    }
}

使用iOS 8 SDK,UIScreen现在面向接口。我想只需删除代码翻转高度和宽度就足以迁移。但这种情况并非如此。用户界面大约在屏幕的三分之一处向右移动并被扭曲。

背景:

  • Xcode 6 beta 6
  • Xamarin.iOS 7.9.4.29
  • 1.8.5

任何有关迁移此代码的建议都将受到赞赏。

2 个答案:

答案 0 :(得分:1)

解决方案是迁移到PlayN 1.9 alpha 1,它现在基于RoboVM而不是Xamarin。

在Info.plist.xml中:

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationLandscapeLeft</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
    <string>UIInterfaceOrientationLandscapeLeft</string>
</array>

启动游戏的java代码(不再需要C#)

public class MetroRoboVM extends UIApplicationDelegateAdapter {

  @Override
  public boolean didFinishLaunching (UIApplication app, UIApplicationLaunchOptions launchOpts) {
    app.setStatusBarStyle (UIStatusBarStyle.LightContent, false);

    RoboPlatform.Config config = new RoboPlatform.Config ();
    config.orients = UIInterfaceOrientationMask.LandscapeLeft; 
    RoboPlatform platform = RoboPlatform.register(app, config);
    addStrongRef(platform);

    String systemVersion = UIDevice.getCurrentDevice().getSystemVersion();
    int indexPoint = systemVersion.indexOf(".");
    if (indexPoint > 0) {
      systemVersion = systemVersion.substring(0,indexPoint);
    }
    int majorVersionNumber = Integer.parseInt(systemVersion);
    boolean isIOS8 = majorVersionNumber >= 8;
    CGSize bounds = UIScreen.getMainScreen().getBounds().size();

    double screenHeight = isIOS8 ? bounds.height() : bounds.width(); 
    double screenWidth  = isIOS8 ? bounds.width()  : bounds.height();

    double heightRatio = screenHeight / MyGame.HEIGHT;
    double widthRatio =  screenWidth / MyGame.WIDTH;
    double ratio = (heightRatio < widthRatio ? heightRatio : widthRatio);
    PlayN.graphics().rootLayer().setScale ((float)ratio, (float)ratio);

    platform.run(new MyGame());
    return true;
 }
...
}

答案 1 :(得分:0)

这对我有用:

public UIWindow window;

在FishedLaunchine

window = new UIWindow(mainViewController.View.Bounds);