Stefan Stefan - 3 months ago 18
Swift Question

Forced Touch with tvOS

Does anybody know how to detect a forced touch/click on the remote control with tvOS?

I want to use the click in a Sprite Kit scene to open a 'game paused alert'. I have no UIKit controls which have the focus and will react on the click.

I'm already using the 'normal' touch events on the remote control to move my sprites around.

Answer

Apple suggests using UIPressesEvent's to detect presses/clicks.

override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
    for item in presses {
        if item.type == .Select {
            self.view.backgroundColor = UIColor.greenColor()
        }
    }
}

override func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
    for item in presses {
        if item.type == .Select {
            self.view.backgroundColor = UIColor.whiteColor()
        }
    }
}

override func pressesChanged(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
    // ignored
}

override func pressesCancelled(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
    for item in presses {
        if item.type == .Select {
            self.view.backgroundColor = UIColor.whiteColor()
        }
    }
}