Search code examples
swifttvos

presses not working tvos - swift


I am trying to detect presses in swift, and I am unable to detect a press. Here is my code:

override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
    print("test1")
}

override func pressesChanged(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
    print("test2")
}

override func pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
    print("test3")
}

override func pressesCancelled(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
    print("test4")
}

After executing the code on a physical apple tv and on a simulator, the console remains empty. Why are presses not being detected? Any help would be appreciated.


Solution

  • I have found the answer, but I am not sure why the override method didn't work.

    let press:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(pressed))
    press.allowedPressTypes = [NSNumber(value: UIPressType.select.rawValue)]
    view.addGestureRecognizer(press)