我的应用程序启动颠倒
我已经开发了一段时间并发布了游戏的引擎现在正在颠倒地启动我当前的项目,并立即按预期的方式旋转 UIView。我使用代码创建界面,其外观如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
CGRect screenBounds = [[UIScreen mainScreen] applicationFrame];
CGRect windowBounds = screenBounds;
windowBounds.origin.y = 0.0;
UIWindow* win = [[UIWindow alloc] initWithFrame:windowBounds];
win.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
UIMyView* view = [[UIMyView alloc] initWithFrame:screenBounds];
UIMyViewController* controller = [[UIMyViewController alloc] init];
controller.view = view;
view.multipleTouchEnabled = true;
view.windowWrapper = this;
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[win addSubview:view];
...
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;
{
s32 validOrientations = ((cViewMM*)(self.view)).windowWrapper->GetValidOrientations();
if (toInterfaceOrientation == UIInterfaceOrientationPortrait && (validOrientations & 0x01))
return true;
if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown && (validOrientations & 0x02))
return true;
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft && (validOrientations & 0x04))
return true;
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight && (validOrientations & 0x08))
return true;
return false;
}
仅当我想在 UIInterfaceOrientationLandscapeLeft 中启动应用程序时,才会出现此问题。通过应用程序进行调试时,看起来当我尝试将视图添加到内部窗口时, shouldAutorotateToInterfaceOrientation: 被多次调用。首先 UIInterfaceOrientationPortrait 返回 false,然后 UIInterfaceOrientationLandscapeRight 返回 true(因为它是有效方向),然后 UIInterfaceOrientationLandscapeLeft 也返回 true(因为它是有效方向并且是设备的当前方向)。
哦,更具体地说,这种情况只发生在 iPhone 上,而不是 iPad 上。他们使用相同的代码来设置此视图。
我做错了什么?
-- 编辑 --
好吧,我对 shouldAutorotateToInterfaceOrientation 的执行是错误的:它执行了 3 次询问 UIInterfaceOrientationPortrait,2 次询问 UIInterfaceOrientationLandscapeLeft,然后一次询问 UIInterfaceOrientationLandscapeRight,再次询问一次 UIInterfaceOrientationLandscapeLeft。
GameCenter 此时已经初始化,并且由于它正在推送一些额外的 UI,我认为可能是这样,但事实并非如此。
An engine that I've worked on for a while and has shipped games is now launching my current project upside down and right away rotates the UIView the way it suppose to be. I create the interface with code and this is about how it looks like:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
CGRect screenBounds = [[UIScreen mainScreen] applicationFrame];
CGRect windowBounds = screenBounds;
windowBounds.origin.y = 0.0;
UIWindow* win = [[UIWindow alloc] initWithFrame:windowBounds];
win.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
UIMyView* view = [[UIMyView alloc] initWithFrame:screenBounds];
UIMyViewController* controller = [[UIMyViewController alloc] init];
controller.view = view;
view.multipleTouchEnabled = true;
view.windowWrapper = this;
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[win addSubview:view];
...
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;
{
s32 validOrientations = ((cViewMM*)(self.view)).windowWrapper->GetValidOrientations();
if (toInterfaceOrientation == UIInterfaceOrientationPortrait && (validOrientations & 0x01))
return true;
if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown && (validOrientations & 0x02))
return true;
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft && (validOrientations & 0x04))
return true;
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight && (validOrientations & 0x08))
return true;
return false;
}
The problem occures only if I want to launch the application in UIInterfaceOrientationLandscapeLeft. Debugging through the application it looks like when I try to add my view to the window internally the shouldAutorotateToInterfaceOrientation: is called multiple times. First with UIInterfaceOrientationPortrait which returns false then UIInterfaceOrientationLandscapeRight which return true (since it's valid orientation) and then UIInterfaceOrientationLandscapeLeft which also returns true (since it's valid orientation and it's the current orientation of the device).
Oh and to be more specific this is happening only on iPhone not on iPad. They use the same code to setup this view.
What am I doing wrong?
-- EDIT --
OK I was wrong about the execution of shouldAutorotateToInterfaceOrientation: it is executed 3 times asking for UIInterfaceOrientationPortrait, 2 times for UIInterfaceOrientationLandscapeLeft, then once for UIInterfaceOrientationLandscapeRight, and again once UIInterfaceOrientationLandscapeLeft.
GameCenter is alreay initializing at this point and since it's pushing some extra UI I thought it may be it, but it wasn't.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
检查 Info.plist 中支持的方向数组是否以纵向(右侧向上)开始。如果它以任何不同的方向启动,您的应用程序将以该方向启动。
Check in your Info.plist that the array of supported orientations starts with portrait (right-side-up). If it starts with any different orientation your app will launch in that orientation.
我也遇到了同样的问题,很遗憾看到它没有得到解答。然后我意识到一件关键的事情证明了事情就是这样运作的:Default.png 启动图像也是颠倒的。您无法在应用程序中执行任何操作来解决此问题。您可以选择正确启动哪种横向模式(左侧或右侧的主页按钮),但不能让它们同时工作。
I was having this same problem and was sad to see it unanswered. Then I realized one key thing that proves this is just how things work: the Default.png startup image is upside down as well. There is nothing you can do in your app to fix that. You can choose which landscape mode starts correctly (home button on left or right) but you can't make them BOTH work.
找到解决方案了!
显然 Interface Builder 中存在错误。如果您只需单击关闭所有方向,然后按所需的顺序重新单击它们,Info.plist 就会正确构建。
Found the solution!
Evidently there is a bug in Interface Builder. If you simply click off all the orientations, then click them back on in the desired order the Info.plist is built correctly.
就我而言,它按照 tgunr 所说工作,但我必须从模拟器文件夹中删除整个应用程序,否则,方向的改变不起作用。
In my case, it worked as tgunr said BUT I had to remove from simulator folder the whole app, otherwise, the change in orientation didn't work.