Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

ios - AVPlayer UITapGestureRecognizer not working

I have this code for my AVPlayerViewController.

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

but this is not working.. :S, I tried setting

[self.avPlayerViewController.view setUserInteractionEnabled:YES];

still no good..

The only working solution is to use UIGestureRecognizer and implement it's shouldReceiveTouch delegate and check if the av player is touched.. but the issue is, we wan't to capture the "tap release" event.. because if the av player view is just touched, it immediately executes the code and that is not what we wanted...

Please help us with this issue..

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The correct place for gesture handling on an AVPlayerViewController is inside the controller's contentOverlayView .

contentOverlayView is a read-only property of AVPlayerViewController. It's a view that shows up over the video, but under the controller. Just the perfect place for touch handling. You can add subviews or gesture handlers to it at load time.

The following code gives your video controller touch support in two ways, to demonstrate both the gesture recognize and UIButton approaches.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"mySegue"])
    {
        // Set things up when we're about to go the the video
        AVPlayerViewController *avpvc = segue.destinationViewController;
        AVPlayer *p = nil;
        p = [AVPlayer playerWithURL:[NSURL URLWithString:@"https://my-video"]];
        avpvc.player = p;

        // Method 1: Add a gesture recognizer to the view
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myAction:)];
        avpvc.contentOverlayView.gestureRecognizers = @[tap];

        // Method 2: Add a button to the view
        UIButton *cov = [UIButton buttonWithType:UIButtonTypeCustom];
        [cov addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
        cov.frame = self.view.bounds;
    }
}

(not really up for writing a Swift version at the moment, sorry. :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...