How do I intercept tapping of the done button in AVPlayerViewController?

前端 未结 10 687
渐次进展
渐次进展 2021-01-03 22:45

I created an AVPlayerViewController and an attached AVPlayer in the viewDidAppear method of a custom UIViewController. Ho

10条回答
  •  北荒
    北荒 (楼主)
    2021-01-03 23:25

    One way is to add additional action to the existing "Done" button on the AVPlayerViewController by searching the respective UIButton inside the subviews of AVPlayerViewController. Once the button is found, add a custom action using addTarget:action:forControlEvents:

    At least this works for me.

    - (UIButton*)findButtonOnView:(UIView*)view withText:(NSString*)text
    {
        __block UIButton *retButton = nil;
    
        [view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if([obj isKindOfClass:[UIButton class]]) {
                UIButton *button = (UIButton*)obj;
                if([button.titleLabel.text isEqualToString:text]) {
                    retButton = button;
                    *stop = YES;
                }
            }
            else if([obj isKindOfClass:[UIView class]]) {
                retButton = [self findButtonOnView:obj withText:text];
    
                if(retButton) {
                    *stop = YES;
                }
            }
        }];
    
        return retButton;
    }
    
    - (void)showPlayer:(AVPlayer*)player
    {
        AVPlayerViewController *vc = [[AVPlayerViewController alloc] init];
        vc.player = player;
    
        [self presentViewController:vc animated:NO completion:^{
            UIButton *doneButton = [self findButtonOnView:vc.view withText:@"Done"];
            [doneButton addTarget:self action:@selector(doneAction:) forControlEvents:UIControlEventTouchUpInside];
            [vc.player play];
        }];
    
    }
    
    - (void)doneAction:(UIButton*)button
    {
        // perform any action required here
    }
    

提交回复
热议问题