简体   繁体   English

在tvOS中未结束的pressesEnd

[英]pressesEnded in tvOS not being called

I am developing a game for tvOS in swift spritekit. 我正在swift spritekit中为tvOS开发游戏。 There is a viewcontroller A which presents a SKScene B. I am forwarding the pressesBegan and pressesEnded from A to B. 有一个显示SKScene B的视图控制器A。我将pressesBegan和pressesEnded从A转发到B。

pressesBegan is being called in A and forwarded to B but pressedEnded is not even called in A. Iam not getting why? 在A中调用了pressesBegan并将其转发给B,但在A中甚至没有调用pressEndEnded。我不明白为什么吗?

Below are the function implemented in A. 以下是在A中实现的功能。

override func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
    if ((self.view as! SKView).scene?.isMemberOfClass(GameScene) == true){
        let gameScene = (self.view as! SKView).scene as! GameScene
        gameScene.pressesEnded(presses, withEvent: event)
    }

} }

override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {

    if ((self.view as! SKView).scene?.isMemberOfClass(GameScene) == true){
        let gameScene = (self.view as! SKView).scene as! GameScene
        gameScene.pressesBegan(presses, withEvent: event)
    }
}

Just ran into this myself. 自己碰到这个。

I believe what is going on is that if you override the method: 我相信这是如果您重写该方法:

- (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event

And don't call super within the method somewhere: 并且不要在方法的某处调用super:

[super pressesBegan:presses withEvent:event];

pressedEnded is also never called. PressedEnded也永远不会被调用。

I had a situation where sometimes pressesEnded was called and sometimes it wasn't, and that was the only difference I could spot. 我遇到的情况是有时调用了presssEnded,有时却没有,这是我能发现的唯一区别。

It looks like there's some sort of bug where pressesEnded doesn't get called in time. 看起来有些bug不能及时调用pressesEnded I noticed by putting a breakpoint on pressesEnded and pressesBegan . 我注意到在pressesEndedpressesBegan上设置了一个断点。 It always stops on pressesBegan , but only stops on pressesEnded after waiting a second to continue. 它始终在pressesBeganpressesBegan ,但仅在pressesEnded停止,等待一秒钟继续。

The only workaround I could find was adding this in viewDidLoad : 我能找到的唯一解决方法是在viewDidLoad中添加它:

let menuPressRecognizer = UITapGestureRecognizer()
menuPressRecognizer.addTarget(self, action: #selector(ShowListViewController.menuButtonAction))
menuPressRecognizer.allowedPressTypes = [NSNumber(value: UIPressType.menu.rawValue)]
self.view.addGestureRecognizer(menuPressRecognizer)

And then this: 然后这个:

func menuButtonAction() {
    print("menu pressed")
    UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
}

That's swift 3 by the way. 顺便说一下,这是3。

I have also ran into this situation. 我也遇到过这种情况。

After some testing it seems that when the 'select' button is released tvOS calls either pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) OR pressesCancelled(_ presses: Set<UIPress>, with event: UIPressesEvent?) . 经过一些测试之后,似乎在释放“选择”按钮时,tvOS调用pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) pressesCancelled(_ presses: Set<UIPress>, with event: UIPressesEvent?)

As to why this is, I don't know. 至于为什么,我不知道。 There isn't anything that I am doing differently between each button press. 每按一次按钮,我所做的任何操作都没有什么不同。 Could be a bug in tvOS. 可能是tvOS中的错误。

I found this solution by jumping to the definition of pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) and finding this comment: 通过跳转到pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?)并找到此注释pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?)我找到了该解决方案:

Your responder will receive either pressesEnded:withEvent or pressesCancelled:withEvent: for each press it is handling (those presses it received in pressesBegan:withEvent:). 对于每个正在处理的印刷机,您的响应者将收到pressesEnded:withEvent或pressesCancelled:withEvent :(在pressesBegan:withEvent:中收到的印刷机)。

Here is a link to the question I asked about the same issue. 这里是一个链接到我问同一个问题的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM