如何在UIWebView视频播放器中添加OverLay视图?

25 video overlay objective-c uiwebview ios

我想在UIWebView视频播放器上添加一些自定义控件.我可以通过以下代码添加对它的任何控制:

首先我在下面添加通知,

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addOverLayView:) name:UIWindowDidBecomeVisibleNotification object:nil]; 
Run Code Online (Sandbox Code Playgroud)

然后收到它,

-(void)addOverLayView:(NSNotification*)aNotification{
    UIWindow *window = (UIWindow *)aNotification.object;

    if (window != self.view.window) {
        [window addSubview:anyCustomview];
    }
}
Run Code Online (Sandbox Code Playgroud)

但是这个视图将静态地放在UIWebView视频视图之上.但我希望实现像视频播放器的控件将隐藏然后我想隐藏我的自定义视图.

换句话说,我想OverLayViewUIWebView视频播放器视图中实现自定义.

Los*_*aty 1

看来“开箱即用”是不可能的。文档指出电影播放器​​在这些情况下会发出通知:

When the movie player begins playing, is paused, or begins seeking forward or backward
When AirPlay playback starts or ends
When the scaling mode of the movie changes
When the movie enters or exits fullscreen mode
When the load state for network-based movies changes
When meta-information about the movie itself becomes available
Run Code Online (Sandbox Code Playgroud)

因此无法知道控件何时隐藏/显示。

如果您只想在用户打开视图后显示一次视图,您可以很容易地实现这一点,例如使用动画:

-(void)addOverLayView:(NSNotification*)aNotification{
    UIWindow *window = (UIWindow *)aNotification.object;

    if (window != self.view.window) {
        [window addSubview:anyCustomview];

        [UIView animateWithDuration:2 //choose a fitting value here
                              delay:3 //this has to be chosen experimentally if you want it to match with the timing of when the controls hide
                            options:UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             anyCustomView.alpha = 0.0f; //fadeout animation, you can leave this block empty to have no animation
                       } completion:^(BOOL finished) {
                             [anyCustomView removeFromSuperview];
                       }];
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,当用户放弃控件时,这不会隐藏您的视图。

如果您想在每次显示/隐藏控件时显示/隐藏视图,那就会变得更加棘手。一种方法是禁用标准控件并重新创建它们 - 请记住,这并不容易。