iPad全屏模式下的MPMoviePlayer覆盖层

5

当视频播放全屏时,我希望在视频播放器上添加一个按钮。我在我的视频播放器上创建了一个覆盖层,在iPhone上运行得非常好。我尝试在iPad上做同样的事情,但按钮从未出现。

这是我的代码:

 NSArray *windows = [[UIApplication sharedApplication] windows];
 if ([windows count] > 1){
       UIWindow * moviePlayerWindow = [windows objectAtIndex:1];
       NSArray * subviews = [moviePlayerWindow subviews];
       UIView * videoView = [subviews objectAtIndex:0];
       [videoView addSubview:myButton];
}

似乎iPad在全屏模式下没有创建UIWindow。

有人知道我该如何做吗?

谢谢!

2个回答

3

几周前我找到了解决这个问题的方法:

似乎这种方法在iPad上不起作用(我还没有检查iPhone SDK 4>),因此为了避免它,您可以执行以下操作。

在添加视频并设置为全屏后,您可以直接将控件添加到UIWindow中(例如[[[[UIApplication sharedApplication] windows] objectAtIndex:0] addSubView:myView]),它们将出现在您的视频之上。

我发现唯一的问题是它们不遵守视图的方向规则,我必须手动编写旋转代码,在视图的willRotateToInterfaceOrientation方法中实现。


1
你能解释一下你是如何处理方向的吗?很高兴能看到一些代码。谢谢。 - Alex1987
我以同样的方式添加了叠加层。你解决了旋转问题吗? - 1110

2

@tigermain提供的解决方案可行。

[[[[UIApplication sharedApplication] windows] objectAtIndex:0] addSubView:myView]

但是添加到窗口中的视图不会跟随方向变化。

解决方案是使用NSNotificationCenter,UIApplicationDidChangeStatusBarOrientationNotification来处理方向变化。

    // assign a notification
    id center = [NSNotificationCenter defaultCenter];
    [center addObserver:self
             selector:@selector(didRotate:)
                 name:UIApplicationDidChangeStatusBarOrientationNotification
               object:nil];

    // this method will get called when the orientation changes
    -(void) didRotate:(NSNotification*)notification {

        UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
        NSLog(@"%d", orientation);
        // ... transform view accordingly to the enum 'UIInterfaceOrientationXXX'
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接