编辑:我最初的解决方案不起作用,因为电影显示在第二个窗口中,位于应用程序主窗口的顶部(在 iPhone 上的视图层次结构中很少有多个窗口)。这个基于Apple 的 MoviePlayer 示例代码的解决方案确实有效:
. . .
// assuming you have prepared your movie player, as in the question
[self.intro play];
NSArray* windows = [[UIApplication sharedApplication] windows];
// There should be more than one window, because the movie plays in its own window
if ([windows count] > 1)
{
// The movie's window is the one that is active
UIWindow* moviePlayerWindow = [[UIApplication sharedApplication] keyWindow];
// Now we create an invisible control with the same size as the window
UIControl* overlay = [[[UIControl alloc] initWithFrame:moviePlayerWindow.frame]autorelease];
// We want to get notified whenever the overlay control is touched
[overlay addTarget:self action:@selector(movieWindowTouched:) forControlEvents:UIControlEventTouchDown];
// Add the overlay to the window's subviews
[moviePlayerWindow addSubview:overlay];
}
. . .
// This is the method we registered to be called when the movie window is touched
-(void)movieWindowTouched:(UIControl*)sender
{
[self.intro stop];
}
注意:您必须将对电影播放器的引用保存在实例变量中,并且声明一个我们可以用来访问它的属性是最方便的。这就是为什么使用self.intro
而不是仅intro
在示例中。如果你不知道如何声明一个实例变量和一个属性,这个网站和其他地方有很多信息。
****下面的原始答案
(在这种情况下不起作用,但在许多类似的情况下,所以我会把它作为一个警告和/或鼓舞人心的例子。)
. . . 如果没有其他工作,我建议子类化 UIWindow 并确保您的应用程序委托实例化它而不是普通的 UIWindow。您可以拦截该类中的触摸并发送通知或直接取消电影(如果您已将指向 MPMoviePlayer 的指针存储在窗口子类的 ivar 中)。
@interface MyWindow : UIWindow {
}
@end
@implementation MyWindow
// All touch events get passed through this method
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
// The screen has been touched, send a notification or stop the movie
return [super hitTest:point withEvent:event];
}
@end