MPMoviePlayer dismiss when pressed
I have a MPMoviePlayer setup to play an intro movie to my application. That works just great, the only problem is that it lasts for 14 seconds, and I want to give my users a chance to skip the intro by pressing anywhere on the movie.
I have hidden the movie controls, as they are not needed.
Code:
NSString *introPath = [[NSBundle mainBundle] pathForResource开发者_运维问答:@"intro" ofType:@"mov"];
intro = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:introPath]];
[intro setMovieControlMode:MPMovieControlModeHidden];
[intro play];
Thank you!
EDIT: My initial solution won't work, because the movie is shown in a second window, layered on top of the app's main window (it's very rare to have more than one window in the view hierarchy on the iPhone). This solution, based on Apple's MoviePlayer sample code, does work:
. . .
// 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];
}
NB: You must save the reference to the movie player in an instance variable, and it's most convenient to declare a property that we can use to access it. That's why is use self.intro
instead of just intro
in the example. If you don't know how to declare an instance variable and a property, there is plenty of information on this site and elsewhere.
**** ORIGINAL ANSWER BELOW
(Doesn't work in this case, but in many similar scenarios, so I'll leave it as a warning and/or inspiring example.)
. . . if nothing else works I'd recommend subclassing UIWindow and making sure that your app delegate instantiates that instead of a normal UIWindow. You can intercept touches in that class and send a notification or cancel the movie directly (if you've stored a pointer to the MPMoviePlayer in an ivar on your window subclass).
@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
精彩评论