【问题标题】:AVPlayer UITapGestureRecognizer not workingAVPlayer UITapGestureRecognizer 不工作
【发布时间】:2016-04-27 01:14:06
【问题描述】:

我的AVPlayerViewController 有这个代码。

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

但这不起作用..:S,我尝试设置

[self.avPlayerViewController.view setUserInteractionEnabled:YES];

还是不行..

唯一可行的解​​决方案是使用 UIGestureRecognizer 并实现它的 shouldReceiveTouch 委托并检查是否触摸了 av 播放器.. 但问题是,我们不想捕获“点击释放”事件.. 因为如果 av player 视图刚刚被触摸,它会立即执行代码,这不是我们想要的......

请帮助我们解决这个问题..

谢谢!

【问题讨论】:

    标签: ios objective-c iphone uitapgesturerecognizer avplayerviewcontroller


    【解决方案1】:

    在 AVPlayerViewController 上进行手势处理的正确位置是在控制器的 contentOverlayView 内。

    contentOverlayView 是 AVPlayerViewController 的只读属性。这是一个显示在视频上方但在控制器下方的视图。只是触摸处理的理想场所。您可以在加载时为其添加子视图或手势处理程序。

    以下代码以两种方式为您的视频控制器触控提供支持,以演示手势识别和 UIButton 方法。

    - (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;
        }
    }
    

    (目前还没有准备好编写 Swift 版本,抱歉。:)

    【讨论】:

    • 我有一个 AVPlayerViewController 负责在自定义 UITableViewCell 中播放视频。将手势识别器添加到contentOverlayView 的方法似乎不起作用。一旦将播放器视图添加到单元格,就会以某种方式拦截触摸。关于我可以做些什么来解决这个问题的任何想法?谢谢!
    • @Stoph 你终于找到解决方案了吗?
    • @ZigiiWong,我不得不回去挖掘我的代码,但我确实找到了我的解决方案。我最终创建了一个自定义 AVPlayerViewController 子类并覆盖了 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event 方法。被覆盖的方法调用一个自定义委托接口,该接口接收触摸事件以执行我需要的任何操作。
    【解决方案2】:

    应该这样做。将识别器添加到播放器的子视图中:

    [videoPlayerViewController.view.subviews[0] addGestureRecognizer:tap]
    

    这是对复杂 UI 和手势的快速修复 contentOverlayView 看 TyR 答案

    【讨论】:

    • 我试图让它工作一周,这就是它所需要的。美丽的。谢谢你,你真的救了我。
    • 在 iOS 11 上,这会导致应用崩溃。
    • 任何人都可以确认它在最终 v11 上仍然崩溃吗?
    • 使用未记录的 SPI。首选方法是使用 contentOverlayView(请参阅我的回答)。
    • 在 iOS 12 上,这会导致应用崩溃。
    【解决方案3】:

    据我所知,你真正想要的是

    UILongPressGestureRecognizer
    

    因为,

    UITapGestureRecognizer
    

    没有任何发布类型的事件,如果你添加了

    UILongPressGestureRecognizer
    

    给你的

    avPlayerViewController
    

    然后在

    -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
      if ( [gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] ) {
    
      }
      return YES;
    }
    

    然后您可以设置手势的最小持续时间以获得更好的感觉,

    [holdGesture setMinimumPressDuration:0.01]
    

    然后就可以实现方法了,

    - (void)holdAction:(UILongPressGestureRecognizer *)holdRecognizer
    

    然后检查识别器的状态并执行所需的功能

    【讨论】:

      【解决方案4】:

      您可以在avplayerViewController 上添加transparent 视图,也可以在该视图上添加tapgesturerecognizer。希望这会有所帮助:)

      【讨论】:

        【解决方案5】:

        简单的解决方案是在添加电影播放器​​后在同一电影持有者视图上添加手势视图。 无需将手势识别器直接添加到电影播放器​​中。

          [self.movieHolderView addSubview:self.moviePlayer.view];
        
        
          [self.movieHolderView addSubview:self.gestureView];
          [self.movieHolderView bringSubviewToFront:self.gestureView];
        

        现在我们可以像这样在手势视图中添加手势识别器

        self.fullScreenTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandler:)];
        self.fullScreenTapGesture.numberOfTapsRequired = 1;
        [self.gestureView addGestureRecognizer:self.fullScreenTapGesture];
        

        【讨论】:

          【解决方案6】:

          斯威夫特 4

          在 AVPlayerViewController 的视图中添加手势识别器:

          func playVideo() {
              let playerController = AVPlayerViewController()
              playerController.player = AVPlayer(url: URL(fileURLWithPath: videoPath))
              playerController.showsPlaybackControls = false
              playerController.view.isUserInteractionEnabled = true
          
              let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
              swipeUp.direction = UISwipeGestureRecognizerDirection.up
              playerController.view.addGestureRecognizer(swipeUp)
          
              present(playerController, animated: false) {
                  playerController.player?.play()
              }
          }
          
          @objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
              print("swipe up")
          }
          

          【讨论】:

            【解决方案7】:

            应该为 OS 11 和更早的操作系统添加不同的手势识别器:

            通用解决方案(Swift 4):

            let tap = UITapGestureRecognizer(target: self, action: #selector(videoTap))
            
            if #available(iOS 11.0, *) {
                playerVC?.contentOverlayView?.gestureRecognizers = [tap]
            } else {
                playerVC?.view.subviews.first?.addGestureRecognizer(tap)
            }
            
            @objc func videoTap(_ button: UIButton) {
                print("Tapped on video")
            }
            

            【讨论】:

              【解决方案8】:

              或者你可以继承 AVPlayerViewController 并使用下面的方法来

              获得控制权:-

              1.touchBegan 2.touchEnd

              确保你设置了这个标志 self.avPlayerContr.showsPlaybackControls = false

              希望这对你有用。

              【讨论】:

              • 覆盖 UIControl 方法是一种 hack。首选方法是使用 contentOverlayView(请参阅我的回答)。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-01-24
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多