MPMoviePlayerController无法识别UITapGestureRecognizer

时间:2015-02-24 22:33:16

标签: ios objective-c mpmovieplayercontroller uitapgesturerecognizer

我试图将UITapGestureRecognizer添加到我的MPMoviePlayerController中,如下所示:

- (void)playVideo {
    NSString *videoURL = self.post[@"videos"][@"high_resolution"];
    videoURL = [videoURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:videoURL];
    self.videoURL = url;

    self.videoController = [[MPMoviePlayerController alloc] init];
    self.videoController.controlStyle = MPMovieControlStyleNone;
    self.videoController.backgroundView.backgroundColor = [UIColor clearColor];

    [self.videoController setContentURL:self.videoURL];
    [self.videoController.view setFrame:CGRectMake (0, 0, 320, 320)];
    [_sharedImage addSubview:self.videoController.view];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playy)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:self.videoController];
    self.playBtn.hidden = YES;
    [self.videoController play];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [_videoController.view addGestureRecognizer:tap];

}

- (void)handleTap:(UITapGestureRecognizer *)gesture {
    [_videoController pause];
    NSLog(@"Video was tapped");
}

#pragma mark - Gesture Delegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

但是MPMoviePlayerController似乎不能识别轻击手势,当我点击视频时没有任何反应。请解释一下。谢谢!

2 个答案:

答案 0 :(得分:1)

您应该设置禁用的互动...

self.videoController.userInteractionEnabled = false;

然后将您的手势添加到超级视图

[self.view addGestureRecognizer:tap];

答案 1 :(得分:0)

您似乎错过了一条重要的路线:

tap.delegate = self;

如果您没有设置代表,代表们将不会被呼叫