example app ios objective-c video uiwebview overlay

ios - app - ¿Cómo agregar OverLay View en el reproductor de video UIWebView?



youtube player swift (4)

Esto es bastante el truco, pero funciona.

// // 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

Quiero agregar algunos controles personalizados sobre el UIWebView de video de UIWebView . Puedo agregar cualquier control sobre él mediante el siguiente código:

Primero estoy agregando abajo la notificación,

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

Entonces al recibirla,

-(void)addOverLayView:(NSNotification*)aNotification{ UIWindow *window = (UIWindow *)aNotification.object; if (window != self.view.window) { [window addSubview:anyCustomview]; } }

Pero esta vista estará estáticamente encima de la UIWebView de UIWebView de UIWebView . Pero quiero lograr lo mismo que cuando los controles del reproductor de video se oculten y luego quiero ocultar mi vista personalizada.

En otras palabras, quiero lograr OverLayView personalizado en la vista del reproductor de video de UIWebView .


Parece que no es posible "fuera de la caja". Los documentos indican que el reproductor de películas emite notificaciones en estas situaciones:

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

Por lo tanto, no hay forma de saber cuándo se ocultaron / mostraron los controles.

Si desea mostrar su vista solo una vez, después de que el usuario la abra, puede lograr esto con bastante facilidad, por ejemplo con la animación:

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

Tenga en cuenta que esto no ocultará su vista cuando el usuario descarte los controles.

Si desea mostrar / ocultar su vista cada vez que se muestran / ocultan los controles, se vuelve mucho más complicado. Un enfoque sería deshabilitar los controles estándar y recrearlos, teniendo en cuenta que no es fácil.


Puede agregar una imagen que sea transparente desde el centro y coloreada desde el borde


Puedes usar el puente iOS-JavaScript para lograr esto.

Puede cargar su secuencia de comandos en WebView que observe algunos elementos o su cambio de propiedad. Y use Bridge para comunicarse entre su script java y el código nativo de iOS.

Puedes echar un vistazo a la referencia de abajo para obtener más ayuda. https://developer.apple.com/library/content/documentation/AppleApplications/Conceptual/SafariJSProgTopics/ObjCFromJavaScript.html#//apple_ref/doc/uid/30001215-BBCBFJCD

También puede agregar la vista de superposición HTML en el reproductor si es lo suficientemente fuerte como para crear dicha vista usando HTML.