在 iPhone 4/4s 的 iOS5 应用程序中,我有一个 UIViewController,它的 View 中添加了一个 MPMoviePlayerController View :
[self.view insertSubview:self.fullscreenMoviePlayerController.view atIndex:2];
UIViewController 仅支持横向:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
{
// Return YES for supported orientations.
return interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}
这正确地将旋转锁定为仅横向。但是,当我将 MPVideoPlayerController 设置为全屏显示时,它会被忽略并且视频不再局限于横向并旋转到手机所持的任何方向。
如何防止 MPMoviePlayerController 的视频在全屏模式下旋转到纵向?当手机旋转为纵向时,视频不会旋转,这一点至关重要。
我已经尝试子类化 MPVideoPlayerController 并覆盖 shouldAutorotateToInterfaceOrientation:
但这没有效果。
MPMoviePlayerController 只是 View 的一部分,因此绝对不能使用 MPMoviePlayerViewCotroller。
最佳答案
如果您真的想避免使用 MPMoviePlayerViewController,这似乎相当困难。 一种选择,即使您在全屏模式下似乎也能工作,是手动设置 MPMoviePlayerController View 的框架。 (请注意,在其他 iOS 问题中,有时使用背景 View 会产生不同的结果,但值得一试)。
MyMPMoviePlayerController.view.frame = CGRectMake(0, 0, your numbers, here);
但是,Apple 在他们的文档中说 Controller 的框架应该设置为其父 View 的框架。
[MyMPMoviePlayerController.view setFrame: parentView.bounds];
不太优雅的解决方案,但即使那个不可行也可能有效的解决方案是:
监听 UIDeviceOrientationDidChangeNotification 并获取电影播放器的 View 。在其上应用变换、边界和中心(或框架等),使其仍然适合横向 View 。每次它试图旋转离开时,基本上都会将其转换回来。 (这一切都假设你真的不能用 shouldAutorotateToInterfaceOrientation: 阻止它旋转)。
这里唯一的问题是它可能会保持电影的纵向但扭曲 View ,这不是预期的结果。
关于ios - 全屏时防止 MPMoviePlayerController 旋转和缩放到纵向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11230628/