Ios 如何将KVO添加到MPMoviePlayerController,以便检测控件何时可见

Ios 如何将KVO添加到MPMoviePlayerController,以便检测控件何时可见,ios,cocoa-touch,mpmovieplayercontroller,Ios,Cocoa Touch,Mpmovieplayercontroller,我希望能够使用MPMoviePlayerController的标准控件使自定义控件出现和消失。最好的方法是什么 谢谢 罗伯我相信我找到了解决办法。如果其他人需要此功能,以下是我如何使其工作的: 我使用找到的代码在MPMoviePlayer视图数组中查找MPInlineVideoOverlay子视图。然后我对它进行了如下修改: -(void)observeValueForKeyPath:(NSString*)键路径 ofObject:(id)对象 更改:(NSDictionary*)更改 上下文

我希望能够使用
MPMoviePlayerController
的标准控件使自定义控件出现和消失。最好的方法是什么

谢谢


罗伯

我相信我找到了解决办法。如果其他人需要此功能,以下是我如何使其工作的:

我使用找到的代码在MPMoviePlayer视图数组中查找MPInlineVideoOverlay子视图。然后我对它进行了如下修改:


-(void)observeValueForKeyPath:(NSString*)键路径
ofObject:(id)对象
更改:(NSDictionary*)更改
上下文:(void*)上下文{

float newValue=0; 如果([change objectForKey:NSKeyValueChangeNewKey]!=[NSNull null]) { newValue=[[ChangeObjectForkey:NSKeyValueChangeNewKey]floatValue]; } NSLog(@“播放器控件可见:%@”,newValue?@“是”:@“否”); self.controlsView.alpha=新值; } -(void)递归视图遍历:(UIView*)视图计数器:(int)计数器{ NSLog(@“深度%d-%@”,计数器,视图);//用于调试 if([视图是类的种类:NSClassFromString(@“MPInlineVideoOverlay”)]){ //在此处添加您希望使用标准控件淡入淡出的任何其他控件 mainControlsView=视图; }否则{ 用于(UIView*子视图中的子视图){ [自递归视图遍历:子计数器:计数器+1]; } } } -(无效)设置附加控件{ //初始化MPMoviePlayerController(可能是viewDidLoad)后调用 mainControlsView=无; [自递归视图遍历:moviePlayer.view计数器:0]; //检查我们是否找到了,如果没有,我们需要在0.1秒内再做一次 if(主控制视图){ [mainControlsView-addObserver:self-forKeyPath:@“alpha”选项:NSKeyValueObservingOptionNew-context:NULL]; }否则{ [自执行选择器:@selector(setupAdditionalControls),对象:nil afterDelay:0.1]; } }`
其中mainControlsView是MPMoviePlayer的标准Apple控件,self.controlsView是我的视图和自定义控件。I键值观察标准控件视图上的alpha属性,并在其更改时更改我的属性以匹配它


Rob

您是否有要使用的自定义控件而不是标准控件?没有,如果可能,我希望在MPMoviePlayer标准控件之外使用自定义控件。我有他们出现,但没有设置如何使他们出现和消失的标准控制。我认为它与MPInlineVideoOverlay视图有关,但无法完全理解如何做我想做的事情。对于iOS 7,相关的类名已更改为
MPVideoPlaybackOverlayView
,要观察的属性是
隐藏的
,因为
alpha
不再更改。

- (void)observeValueForKeyPath:(NSString *)keyPath 
                          ofObject:(id)object 
                            change:(NSDictionary *)change 
                           context:(void *)context {

float newValue = 0; if([change objectForKey:NSKeyValueChangeNewKey] != [NSNull null]) { newValue = [[change objectForKey:NSKeyValueChangeNewKey] floatValue]; } NSLog(@"player controls are visible: %@", newValue ? @"YES" : @"NO"); self.controlsView.alpha = newValue; } -(void)recursiveViewTraversal:(UIView*)view counter:(int)counter { NSLog(@"Depth %d - %@", counter, view); //For debug if([view isKindOfClass:NSClassFromString(@"MPInlineVideoOverlay")]) { //Add any additional controls you want to have fade with the standard controls here mainControlsView = view; } else { for(UIView *child in [view subviews]) { [self recursiveViewTraversal:child counter:counter+1]; } } } -(void)setupAdditionalControls { //Call after you have initialized your MPMoviePlayerController (probably viewDidLoad) mainControlsView = nil; [self recursiveViewTraversal:moviePlayer.view counter:0]; //check to see if we found it, if we didn't we need to do it again in 0.1 seconds if(mainControlsView) { [mainControlsView addObserver:self forKeyPath:@"alpha" options:NSKeyValueObservingOptionNew context:NULL]; } else { [self performSelector:@selector(setupAdditionalControls) withObject:nil afterDelay:0.1]; } }`