MPMoviePlayerController's view does not recognize touch

beryllium picture beryllium · Sep 20, 2011 · Viewed 13.7k times · Source

This is my code:

_mediaPlayer = [[MPMoviePlayerController alloc] init];
_mediaPlayer.controlStyle = MPMovieControlStyleNone;
_mediaPlayer.shouldAutoplay = NO;
[_mediaPlayer.view setFrame: CGRectMake(5, 5, 600,400)];
[playerHolder addSubview: _mediaPlayer.view];
//
[self prepareScreenContentToPlay];
//
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleRollTap:)];
singleFingerTap.numberOfTapsRequired = 1;
[_mediaPlayer.view addGestureRecognizer:singleFingerTap];
[singleFingerTap release];

And action method for gesture recognizer:

-(void)handleRollTap:(UITapGestureRecognizer*)sender{
    NSLog(@"%@", @"touch");
}

MPMoviePlayerController works fine. In addition I want to handle touch on MPMoviePlayerController view but handleRollTap never called. Why MPMoviePlayerController's view not works with UITapGestureRecognizer?


OK. If singleFingerTap.numberOfTapsRequired = 2; then all works fine as well. But nothing for single tap..


Answer

mdomans picture mdomans · Oct 15, 2012

Actually, answer to this is simple:

  • set yourself as UIGestureRecognizer delegate
  • return YES for delegate methods:

e.g.

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture)];
tapGestureRecognizer.delegate = self;

and somewhere else in the code:

#pragma mark - gesture delegate
// this allows you to dispatch touches
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return YES;
}
// this enables you to handle multiple recognizers on single view
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}