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

时间:2011-08-23 15:25:42

标签: ios cocoa-touch mpmovieplayercontroller

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

谢谢,

罗布

1 个答案:

答案 0 :(得分:4)

我相信我找到了解决方案。如果其他人需要这个功能,这就是我如何使用它:

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


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

其中mainControlsView是MPMoviePlayer的标准Apple控件,self.controlsView是我的自定义控件的视图。 I Key Value观察标准控件视图上的alpha属性,并在我发生更改时将其更改为匹配。

罗布