The tvOS long press

Random

If you’d like to detect a long press on a UIButton or just for a view that has no buttons – it’s pretty easy. There is one simple gotcha, however. You’ll trigger your selector twice. Once when detected (down on the glass pad Apple TV remote), and again on the pad’s release.

So you’ll need to set a BOOL for knowing what the state is. Easy enough.

fileprivate var longDown: Bool = false

override func viewDidLoad()
{
    super.viewDidLoad()
    let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, 
    action: #selector(longPress(longPressGestureRecognizer:)))
    view.addGestureRecognizer(longPressGestureRecognizer)
}

func longPress(longPressGestureRecognizer : UILongPressGestureRecognizer)
{
    // I fire on the down and also on the release.
        
    if longDown == false {
        longDown = true
        print("long press down.")
    } else {
        longDown = false
        print("long press up.")
    }
}

I would not have thought that the gesture would be triggered twice on hardware, but it does.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.