UITapGestureRecognizer & tvOS remote for Pause/Play Button

喜欢而已 提交于 2019-12-13 19:40:44

问题


I am trying to create an action when a user clicks the pause/play button on the Apple TV Remote. I looked at the documentation, but the code Apple's documentation recommended does not work. Here is my code below. Could someone please tell me what I am doing wrong? Things to consider: I am using an AVPlayerMovieController & I do have another gesture recognizer in my code but for it is a swipe gesture AND this method is getting called, just not the Pause/Play. Can someone please help?

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

Thank you


回答1:


You can override the pressesEnded method:

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

    }
}



回答2:


I have the same issue and I found solution but to be honest I don't like it at all (but at least it is working for me).

In view did load I added

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

UIApplication.sharedApplication().beginReceivingRemoteControlEvents()

and I have two methods implemented:

    func playPauseButtonPressed(sender: AnyObject) {
    self.buttonPlayClicked(buttonPlay)
    print("PRESSED")
}

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

I am pretty sure that I am not doing something right but I couldn't find solution anywhere. This is just my improvisation that works for me.

Hope this helps.




回答3:


I've used the following, and my recogniser get's called. I think you need to get the rawValue of the PlayPause UIPressType instead of just the PlayPause UIPressType.

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

view.addGestureRecognizer(playPauseRecognizer)



回答4:


I have the same issue where play/pause events are not working in view controllers. The workaround was to add play/pause UITapGestureRecognizer in UIWindow in appdelegate and broadcast the event using notification.



来源:https://stackoverflow.com/questions/33506773/uitapgesturerecognizer-tvos-remote-for-pause-play-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!