所以我正在构建的应用程序遇到了这个问题,我无法弄清楚。
我的应用程序具有以下结构:
UITabBarController -> UIViewController -> UIViewController
最后一个视图控制器包含一个加载整个页面的 UIWebView。在该页面中,有一个电影(mp4),用户可以在点击大圆形播放按钮后播放。
该应用程序被设计为以纵向模式运行,并且由于它的初始设计,我无法以其他方式运行它。
我想要实现的是让用户在电影开始播放后旋转手机并相应地旋转电影。我已经通过监听 NSNotificationsCenter 发送的不同 NSNotifications 设法“检测”电影何时播放。
我用它来检测开始:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieIsPlaying:)
name:@"UIMoviePlayerControllerDidEnterFullscreenNotification"
object:nil];
这是为了检测结束:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieStopedPlaying:)
name:@"UIMoviePlayerControllerDidExitFullscreenNotification"
object:nil];
现在,有了 2 个选择器,我将一个全局变量设置为 YES 或 NO,稍后我可以使用它
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
如果我总是从上述方法返回 YES,我的应用程序界面将在每个屏幕上旋转,除非电影启动并运行。最奇怪的是:
- 我以纵向方式打开应用程序。
- 转到所需的页面(请参阅上面我的结构)
- 将手机旋转至横向(界面旋转-这只是为了调试)
- 我开始播放电影(它以横向模式启动!万岁!)
- 我将手机旋转回纵向(电影旋转!万岁!)
- 我将手机旋转回横向(不再发生旋转......)
另外,如果我以纵向模式启动电影,它永远不会旋转。
Update
最后我针对上述两种方法提出了这个解决方案:
- (void)movieIsPlaying:(NSNotification *)notification
{
if (![Globals canRotateInterface]) {
[Globals setCanRotateInterface:YES];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
[[[[notification object] view] window] setBounds:CGRectMake(0, 0, 480, 320)];
[[[[notification object] view] window] setCenter:CGPointMake(160, 240)];
[[[[notification object] view] window] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
}
}
- (void)movieStopedPlaying:(NSNotification *)notification
{
if ([Globals canRotateInterface]) {
[Globals setCanRotateInterface:NO];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
[[[[notification object] view] window] setBounds:CGRectMake(0, 0, 320, 480)];
[[[[notification object] view] window] setTransform:CGAffineTransformIdentity];
// Little hack to force an orientation rotation so that all elements get their original size and place (eg. Navigation bar)
UINavigationController *dummyNavigationViewController = [[UINavigationController alloc] init];
[self presentModalViewController:dummyNavigationViewController animated:NO];
[self dismissModalViewControllerAnimated:NO];
[dummyNavigationViewController release];
}
}