I have a UICollectionViewController in shared iOS/tvOS code. On iOS I use context menus while I fallback to UIAlertController (alert sheet) triggered by a long press on tvOS.
viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
// ...
#if os(tvOS)
let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressAction(_:)))
collectionView.addGestureRecognizer(longPressGestureRecognizer)
#endif
}
Selector:
#if os(tvOS)
@objc func longPressAction(_ sender: UIGestureRecognizer) {
guard sender.state == .began else {
return
}
if let indexPath = collectionView.indexPathForItem(at: sender.location(in: collectionView)) {
let ac = UIAlertController(/* ... */) // alert sheet
present(ac, animated: true, completion: nil)
}
}
#endif
Upon long pressing, the alert sheet gets displayed properly on tvOS but the first tap on one of the sheet actions is ignored (this calls longPressAction again with a .cancel state). I tried inserting a sender.isEnabled = false after the guard but it simply caused the .cancel state to come in faster while still eating the first tap.
Suggestions?