tvOS: Is there anyway to detect a long press of an arrow button?

So I've been trying to detect a long press of one of the Apple TV remote arrow buttons (triggered when touching the edges of the touch pad) and it seems like it doesn't work for the arrows, only for the physical buttons you click down. Basically:

// This works
let longSelectRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressed(_:)))
longSelectRecognizer.allowedPressTypes = [UIPressType.Select.rawValue)]
self.addGestureRecognizer(longSelectRecognizer)

// This doesn't
let longArrowRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressed(_:)))
longArrowRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.LeftArrow.rawValue), NSNumber(integer: UIPressType.RightArrow.rawValue)]
self.addGestureRecognizer(longArrowRecognizer)

I tried replacing it with a UITouchGestureRecognizer, but it doesn't detect when holding down the arrow (as expected)

Upvotes: 1

Views: 2115

Answers (1)

mr2k
mr2k

Reputation: 438

I believe this is because down/up/right/left-arrows are virtual presses. They are re-broadcasted from touchesBegan/Ended.

The only way you can check for long presses on down/up/right/left is calculating touch.timestamp in touchesBegan/Ended.

var touchStart: TimeInterval?

    override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        touches.forEach({ touch in
            switch touch.type{
                case .indirect:
                    self.touchStart = touch.timestamp
                break
                default:
                break
            }

        })
    }

    override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        touches.forEach({ touch in
            switch touch.type{
                case .indirect:
                    if
                        let start = self.touchStart,
                        (touch.timestamp - start < 0.5){
                        super.touchesEnded(touches, with: event)
                    }else {
                        self.performSegue(withIdentifier: SegueEnum.showOverlaySegue.rawValue, sender: nil)
                    }
                break
                default:
                break
            }
        })
    }

Upvotes: 1

Related Questions