2015-11-03 57 views
0

我试图在用户单击Apple TV遥控器上的暂停/播放按钮时创建操作。我查看了文档,但Apple推荐的文档代码不起作用。以下是我的代码如下。有人能告诉我我做错了什么吗?需要考虑的事项: 我正在使用AVPlayerMovieController &我的代码中有另一个手势识别器,但它是一个轻扫手势,此方法正在调用,而不是暂停/播放。有人可以帮忙吗?UITapGestureRecognizer&tvOS遥控器用于暂停/播放按钮

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

谢谢

回答

2

您可以覆盖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) 
     } 

    } 
} 
+0

如果您的视图已经在检测按下按钮(例如UICollectionViewCell等),它将起作用(无需添加UITapGestureRecognizer)。所有其他条件相同,最简单的解决方案应该是选定的答案(这一个)。对于其他人,您可以添加点按手势识别器,并允许按下UIPressType.PlayPause按钮。请参阅https://developer.apple.com/library/tvos/documentation/General/Conceptual/AppleTV_PG/DetectingButtonPressesandGestures.html – skim

2

我用以下内容,我识别器获取的调用。我认为你需要获取PlayPause UIPressType的rawValue,而不仅仅是PlayPause UIPressType。

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

view.addGestureRecognizer(playPauseRecognizer) 
+0

您是否尝试过使用设备?它在模拟器上工作,但不在设备上。 – quaertym

+0

是的,我用设备试了一下,它的工作。 –

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") 
} 

我敢肯定,我没有做正确的事情,但我找不到任何地方解决方案。 这只是我的即兴作品。

希望这会有所帮助。

+0

它不在仿真器上工作,您是在仿真器上还是在实际设备上测试它? – Nick

+0

我在几台设备上测试过它,它工作正常。在模拟器中,当我实现了只有轻击识别器时它工作得很好,但是当我尝试它时,它在设备上不起作用。 –

+0

有趣......我测试过,如果在实际的设备上,它确实工作。去重新安装Xcode,看看它是否有模拟器的问题。谢谢 – Nick

0

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