如何旋转UIWebView嵌入视频 [英] How to rotate an UIWebView embedded video

查看:135
本文介绍了如何旋转UIWebView嵌入视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对我正在构建的应用程序有这个问题,我无法理解。
我的应用程序具有以下结构:


UITabBarController - > UIViewController - > UIViewController


最后一个视图控制器包含一个加载整个页面的UIWebView。在该页面中有一个电影(mp4),用户可以在点击大圆形播放按钮后播放。



应用程序是以纵向模式运行的,由于它的初始设计,我无法做到这一点。



我想要实现的是让用户在电影开始播放和旋转后旋转手机相应的电影。我通过收听NSNotificationsCenter发送的不同NSNotifications来播放电影时设法'检测'。



我用它来检测开始:

  [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(movieIsPlaying :)
name:@UIMoviePlayerControllerDidEnterFullscreenNotification
对象:nil];

这用于检测结束:

  [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(movieStopedPlaying :)
name:@UIMoviePlayerControllerDidExitFullscreenNotification
object:nil];

现在使用2个选择器我将一个全局变量设置为YES或NO我将在稍后使用

   - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 

如果我总是从上面的方法返回YES,我的应用程序界面会在everyscreen上旋转,除非电影启动并运行。最奇怪的是:




  • 我以纵向打开应用程序。

  • 转到所需的页面(参见上面的结构)

  • 将手机旋转为横向(界面旋转 - 这仅用于调试)

  • 我启动电影(它开始在风景中!Horray!)

  • 我将手机旋转回肖像(电影旋转!Horray再次!)

  • 我旋转手机回到风景(不再发生旋转...)



此外,如果我以纵向模式启动电影,它将永远不会旋转。 / p>

所以,如果你能以某种方式帮助我,请各位让我知道。我真的很绝望。如果您需要更多信息,请告诉我。



谢谢!



更新



最后,我为上述2种方法提出了这个解决方案:

   - (void)movieIsPlaying:(NSNotification *)notification 
{
if(![Globals canRotateInterface]){
[Globals setCanRotateInterface:YES];

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];

[[[[对象]视图]窗口] setBounds:CGRectMake(0,0,480,320)];
[[[[对象]视图]窗口] setCenter:CGPointMake(160,240)];
[[[[对象]视图]窗口] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
}
}

- (无效)movieStopedPlaying:(NSNotification *)通知
{
if([Globals canRotateInterface]){
[Globals setCanRotateInterface:NO];

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];

[[[[对象]视图]窗口] setBounds:CGRectMake(0,0,320,480)];
[[[[对象]视图]窗口] setTransform:CGAffineTransformIdentity];

//强制定位旋转以便所有元素都获得原始大小和位置(例如导航栏)
UINavigationController * dummyNavigationViewController = [[UINavigationController alloc] init];
[self presentModalViewController:dummyNavigationViewController animated:NO];
[self dismissModalViewControllerAnimated:NO];
[dummyNavigationViewController release];
}
}


解决方案

什么我最后做的是使用2个提到的通知来检测用户何时播放或停止电影,并具有将窗口旋转到90度的代码。这样,用户只能以横向模式观看电影,但它比仅以肖像方式看电影更好。


So I have this problem with an application I am building that I can't figure out. My application has the following structure:

UITabBarController -> UIViewController -> UIViewController

The last view controller contains an UIWebView which loads an entire page. In that page there is a movie (mp4) that users can play once they hit the big round play button.

The application is made such that it runs in portrait mode, and there is no way I can do it run otherwise because of it's initial design.

What I want to achieve is let users rotate their phones once the movie starts playing and rotate the movie accordingly. I've managed to 'detect' when the movie is played by listening to different NSNotifications that NSNotificationsCenter sends.

I used this for detecting the start:

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieIsPlaying:)
name:@"UIMoviePlayerControllerDidEnterFullscreenNotification"
object:nil];

and this for detecting the end:

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieStopedPlaying:)
name:@"UIMoviePlayerControllerDidExitFullscreenNotification"
object:nil];

With the 2 selectors now I set one global variable to YES or NO wich I can use later in

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

If I always return YES from the above method my application interface rotates on everyscreen except when the movie is up and running. The weirdest thing is this:

  • I open the application in portrait.
  • Go to the desired page (see my structure above)
  • Rotate the phone to landscape (the interface rotates - this is just for debugging)
  • I start the movie (it launches in landscape! Horray!)
  • I rotate the phone back to portrait (the movie rotates! Horray again!)
  • I rotate the phone back to landscape (no more rotation occurs...)

Also if I start the movie in portrait mode it will never rotate.

So please guys let me know if you can help me somehow. I'm really desperate. If you need any more information please let me know.

Thanks!

Update

In the end I've come up with this solution for the 2 methods mentioned above:

- (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];
    }
}

解决方案

What I did in the end was use the 2 mentioned notifications to detect when the user plays or stops the movie and have code that rotates the window to 90 degrees. This way the user only sees the movie in landscape mode but it's better than just seeing it in portrait.

这篇关于如何旋转UIWebView嵌入视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆