Not covered in book: Accessing TVML elements' views from Swift

The tvOS Apprentice book is really an outstanding title! However, there was something that I didn’t see covered in it and I haven’t found an answer to anywhere else.

I am showing a list of items using the TVML “Catalog” template. Users can delete items from the list, so I want to add a “shake” animation to a TVViewElement’s UIView when the user performs a “holdselect” action on it, like an app’s icon does when you’re about to delete it from the main Apple TV screen. I haven’t found built-in functionality that provides this behavior.

Based on what I learned from the tvOS Apprentice book, I think I need to call a Swift function from TVJS in response to the holdselect event being dispatched for a selected item. The Swift function will apply the animation to the selected item’s UIView. To do this, the UIView must be provided to it from TVJS or the UIView must be located given the selected TVViewElement. I don’t see a way to retrieve the UIView that is associated with a given TVViewElement.

Does anyone know how to do this? Feel free to suggest a different approach. Thanks!

FYI, according to Apple Dev staff, there isn’t a built-in way of doing this.

Nurinder M
Mar 14, 2016 9:58 AM

There isn’t any elegant way for you to acheive this using AppleTV constructed views. You would need to implement TVInterfaceCreating and create your own views and handle gestures on it.

Hi tommyb,

Sorry about the delay in my reply. I’m glad that my thoughts on the answer were backed up by Apple’s reply.

You could potentially override the generation of an Apple-based view and maintain some kind of lookup of native views and TVML elements, but I’m not sure about the viability of this approach without digging into it.

sam

Thanks for the reply, Sam. I’m surprised that this wasn’t implemented “out of the box” yet. I expected the integration needed to allow shoring up TVML capabilities on the Swift side to be available.

It seems like going either direction, i.e., getting the TVViewElement associated with a UIView or vice versa, could be useful in a lot of situations where TVML almost does what you need.