【问题标题】:UITapGestureRecognizer & tvOS remote for Pause/Play Button用于暂停/播放按钮的 UITapGestureRecognizer 和 tvOS 遥控器
【发布时间】:2015-11-03 18:47:04
【问题描述】:

当用户单击 Apple TV Remote 上的暂停/播放按钮时,我正在尝试创建一个动作。我查看了文档,但 Apple 文档推荐的代码不起作用。下面是我的代码。有人可以告诉我我做错了什么吗?需要考虑的事项: 我正在使用 AVPlayerMovieController 并且我的代码中确实有另一个手势识别器,但因为它是滑动手势并且此方法被调用,而不是暂停/播放。有人可以帮忙吗?

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

谢谢

【问题讨论】:

    标签: objective-c uitapgesturerecognizer tvos apple-tv


    【解决方案1】:

    您可以覆盖 pressesEnded 方法:

    override func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
        for press in presses {
            if(press.type == UIPressType.PlayPause) {
               // Do what you want
               // ...
            } else {
                super.pressesEnded(presses, withEvent: event)
            }
    
        }
    }
    

    【讨论】:

    • 如果您的视图已经检测到按下(例如 UICollectionViewCell 等),则此方法有效(无需添加 UITapGestureRecognizer)。在其他条件相同的情况下,最简单的解决方案应该是选择的答案(这个)。对于其他人,您可以添加允许按下类型为 UIressType.PlayPause 的点击手势识别器。见developer.apple.com/library/tvos/documentation/General/…
    【解决方案2】:

    我已经使用了以下内容,并且我的识别器被调用了。我认为您需要获取 PlayPause UIPressType 的 rawValue 而不仅仅是 PlayPause UIPressType。

    let playPauseRecognizer = UITapGestureRecognizer(target: self, action: "playPauseRecognizer:")
    playPauseRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.PlayPause.rawValue)]
    
    view.addGestureRecognizer(playPauseRecognizer)
    

    【讨论】:

    • 你用设备试过了吗?它适用于模拟器,但不适用于设备。
    【解决方案3】:

    我有同样的问题,我找到了解决方案,但老实说我一点也不喜欢它(但至少它对我有用)。

    在视图中我添加了加载

    let tapRecognizer = UITapGestureRecognizer(target: self, action: "playPauseButtonPressed:")
        tapRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.PlayPause.rawValue)];
        self.view.addGestureRecognizer(tapRecognizer)
    
    UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
    

    我实现了两种方法:

        func playPauseButtonPressed(sender: AnyObject) {
        self.buttonPlayClicked(buttonPlay)
        print("PRESSED")
    }
    
    override func remoteControlReceivedWithEvent(event: UIEvent?) {
        self.buttonPlayClicked(buttonPlay)
        print("EVENT")
    }
    

    我很确定我做的不对,但我在任何地方都找不到解决方案。 这只是我的即兴创作,对我有用。

    希望这会有所帮助。

    【讨论】:

    • 在模拟器上不行,你是在模拟器上测试还是在实际设备上测试?
    • 我在几台设备上对其进行了测试,它工作正常。在模拟器中,当我只实现点击识别器时它运行良好,但当我尝试它时它在设备上不起作用。
    • 有趣...我在实际设备上进行了测试,它确实有效。重新安装 Xcode 看看是不是模拟器的问题。谢谢
    【解决方案4】:

    我有同样的问题,即播放/暂停事件在视图控制器中不起作用。解决方法是在 appdelegate 的 UIWindow 中添加播放/暂停 UITapGestureRecognizer 并使用通知广播事件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-29
      • 1970-01-01
      • 2020-08-31
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多