如何解决iPad应用程序中的定向问题
| 我正在创建一个应用程序,在该应用程序中,我需要播放由mpmovieplayer控制器执行的视频。现在我需要同时针对两个方向进行此操作。但是框架设置不正确。 该代码如下// Implement viewDidLoad to do additional setup after loading the view,typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[self shouldAutorotatetoInterfaceOrientation:[UIDevice currentDevice].orientation];
NSURL *temp = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@\"Floating\" ofType:@\"mp4\"]];
mpviewController = [[MPMoviePlayerViewController alloc] initWithContentURL:temp];
mpviewController.view.frame = CGRectMake(0,768,1024);
mpviewController.view.backgroundColor = [UIColor clearColor];
mpviewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
mpviewController.view.userInteractionEnabled= NO;
mpviewController.moviePlayer.fullscreen= YES;
mpviewController.moviePlayer.controlStyle = MPMovieControlStyleNone;
[[mpviewController moviePlayer] play];
[self.view addSubview:mpviewController.view];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotatetoInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
currentOrientation = interfaceOrientation;
//[self SetInterface];
if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
mpviewController.view.frame = CGRectMake(0,1024);
else if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
mpviewController.view.frame = CGRectMake(0,1024,768);
return YES;
}
我不知道我在哪里错。请让我知道代码中有什么麻烦。以便获得正确的方向。
拉加德·阿比(Ragards Abhi)
解决方法
第一
我相信您不需要调整mpviewController的大小,因为它可以自行调整大小。
您只应设置-
第二
在shouldAutorotateToInterfaceOrientation中,您只能在shouldAutorotateToInterfaceOrientation中设置受支持的方向。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
如果不这样做,您可以在-中更改视图属性
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if (UIInterfaceOrientationIsPortrait(interfaceOrientation)){
//do what you want for portrait
}else{
//do what you want for landscape
}
}
, 您只应在shouldAutorotateToInterfaceOrientation:
方法中返回YES或NO,它仅被框架调用以获取有关控制器中支持的方向的信息,请阅读Apple文档。
您需要注册以获取方向更改通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@\"UIDeviceOrientationDidChangeNotification\" object:nil];
实现您的orientationChanged:
方法。
//********** ORIENTATION CHANGED **********
- (void) orientationChanged:(NSNotification *)note
{
NSLog(@\"Orientation has changed: %d\",[[note object] orientation]);
//Put your code here from shouldAutorotateToInterfaceOrientation:
}
不要忘记将其删除。
[[NSNotificationCenter defaultCenter] removeObserver:self];
这是一些链接
设备方向-自动旋转?
方向更改通知
, 无需更改任何编码..只需将以下编码插入应用程序,它将自动检测方向...
UINavigationBar * bar = [self.navigationController navigationBar];
[bar setTintColor:[UIColor blackColor]];
NSBundle * bundle = [NSBundle mainBundle];
NSString * moviePath = [bundle pathForResource:@ \“ sharkdivertrailer \” ofType:@ \“ mp4 \”];
NSURL * movieURL = [[NSURL fileURLWithPath:moviePath]保留];
MPMoviePlayerController * theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.view.frame = CGRectMake(184,200,400,300);
[电影播放];
MPMoviePlayerViewController * moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。