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

29

我想为 UIWebView 的视频播放器添加一些自定义控件。我能够通过以下代码添加任何控件:

首先,我添加了以下通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addOverLayView:) name:UIWindowDidBecomeVisibleNotification object:nil]; 

然后在收到它之后,

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

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

但是这个视图将静态地位于 UIWebView 的视频视图之上。但我想要实现的是,当视频播放器的控件隐藏时,我希望隐藏我的自定义视图。

换句话说,我想在 UIWebView 的视频播放器视图上实现自定义的 OverLayView


从window.subviews获取视图引用并同时删除该视图。 - Alok
为什么不使用其他自定义视频播放器? - M.Alatrash
4个回答

1
你可以使用iOS-JavaScript桥来完成此操作。
您可以在WebView中加载脚本,观察某些元素或其属性的更改,并使用桥梁在JavaScript和本机iOS代码之间进行通信。
您可以查看下面的参考资料以获取更多帮助。 https://developer.apple.com/library/content/documentation/AppleApplications/Conceptual/SafariJSProgTopics/ObjCFromJavaScript.html#//apple_ref/doc/uid/30001215-BBCBFJCD 如果您足够强大,还可以在播放器上添加HTML覆盖视图。

1

您可以添加一张中心透明、边缘着色的图片


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

所以没有办法知道控件何时隐藏/显示。
如果你想在用户打开视图后只显示一次,你可以很容易地实现这个目标,例如通过动画:
-(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];
                       }];
    }
}

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

我不想让视图仅出现一次。该视图必须与UIWebView播放器上的控件同步。 - user4202109
好的,看起来为您实现一整套自定义控件是可行的方案。正如我之前所说,这并不是一项容易的任务。 - Losiowaty
是的,这不会是一件容易的事情 :) - user4202109

1
这种方法相当独特,但确实有效。
//
//  ViewController.m
//

#import "ViewController.h"

@interface ViewController ()

@property UIWebView *webView;

@property UIWindow *playerWindow;
@property UIView *customView;
@property UIView *transitionView;
@property NSTimer *timer;

@end

@implementation ViewController

-(void)loadView
{
    self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:_webView];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addOverLayView:) name:UIWindowDidBecomeVisibleNotification object:nil];

}

-(void)addOverLayView:(NSNotification*)aNotification
{
    UIWindow *window = (UIWindow *)aNotification.object;
    _customView = [UIView new];
    _customView.backgroundColor = [UIColor yellowColor];
    _customView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 80, [UIScreen mainScreen].bounds.size.width, 80);
    _customView.alpha = 0.0;
    if (window != self.view.window)
    {
        _playerWindow = window;
        [_playerWindow addSubview:_customView];
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
    }
}

- (void)timerMethod
{
    if(_transitionView)
    {
        UIView *view = [_transitionView.subviews lastObject];
        view = [view.subviews lastObject];
        for(UIView *subView in view.subviews)
        {
            if([subView.layer.sublayers count])
            {
                CALayer *finalLayer = [subView.layer.sublayers lastObject];
                CAAnimation *animation = [finalLayer animationForKey:@"opacity"];
                if(animation)
                {
                    if(finalLayer.opacity)
                    {
                        [UIView animateWithDuration:animation.duration delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                        _customView.alpha = 1.0;} completion:nil];
                    }
                    else
                    {
                        [UIView animateWithDuration:animation.duration delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                            _customView.alpha = 0.0;} completion:nil];
                    }
                } 
            }
        }
    }
    else
    {
        for(id view in _playerWindow.subviews)
        {
            NSString *className = [NSString stringWithFormat:@"%@", [view class]];
            if([className isEqualToString:@"UITransitionView"])
            {
                _transitionView = view;
                break;
            }
        }

    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *fullURL = @"http://www.youtube.com";
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:requestObj];
}

@end

嗨,CustomView只出现一次。如果我关闭视频并播放另一个视频,则不会显示。 - user4202109

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