Reputation: 111
I am developing a game for tvOS in swift spritekit. There is a viewcontroller A which presents a SKScene B. I am forwarding the pressesBegan and pressesEnded from A to B.
pressesBegan is being called in A and forwarded to B but pressedEnded is not even called in A. Iam not getting why?
Below are the function implemented in 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)
}
}
Upvotes: 4
Views: 2209
Reputation: 507
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?)
.
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.
I found this solution by jumping to the definition of pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?)
and finding this comment:
Your responder will receive either pressesEnded:withEvent or pressesCancelled:withEvent: for each press it is handling (those presses it received in pressesBegan:withEvent:).
Here is a link to the question I asked about the same issue.
Upvotes: 0
Reputation: 655
It looks like there's some sort of bug where pressesEnded
doesn't get called in time. I noticed by putting a breakpoint on pressesEnded
and pressesBegan
. It always stops on pressesBegan
, but only stops on pressesEnded
after waiting a second to continue.
The only workaround I could find was adding this in 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.
Upvotes: 0
Reputation: 1843
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 pressesBegan:presses withEvent:event];
pressedEnded is also never called.
I had a situation where sometimes pressesEnded was called and sometimes it wasn't, and that was the only difference I could spot.
Upvotes: 0