Search code examples
swiftcocoa-touchtvos

What types of UI elements can respond to gestures in tvOS?


I'm new to tvOS and trying to make my app more interactive by adding gesture recognizers to bunch of UI elements, but noticed that I can't do it with some of them.

I noticed that UIButton, UICollectionView or UITableView are the ones that can recognize gestures, but if I create a custom view and add a custom gesture recognizer to it, it doesn't work.

Am I doing something wrong, or there is a list of UI elements that can respond to gestures?!

Any kind of help is highly appreciated.

import UIKit

class TouchViewController: UIViewController{

@IBOutlet weak var myView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

    let swipeDown:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedDown:"))
    swipeDown.direction = .Down
    myView.addGestureRecognizer(swipeDown)

    let tap = UITapGestureRecognizer(target: self, action: "itemTapped:")
    tap.allowedPressTypes = [NSNumber(integer: UIPressType.Select.rawValue)]
    myView.addGestureRecognizer(tap)

}

func itemTapped(gesture: UITapGestureRecognizer){
    print("tapped") 
}

func swipedDown(sender:UISwipeGestureRecognizer){
    print("swiped")
}

Solution

  • Every kind of view can recognize gestures, there's nothing special about the classes you mentioned.

    Your gestures must not be working for some other reason: can you elaborate on what you're trying to do, and provide some sample code?

    Updated based on seeing your code:

    Make sure the view that you're attaching these gestures to either (1) is focused, or (2) contains the focused view. Events are delivered to the focused view and may "bubble up" the responder chain from there. If your custom view doesn't contain the focused view, then it won't see any of those events.