Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate/zoom a focused custom view in tvOS

Tags:

focus

tvos

I have a custom UICollectionViewCell in my tvOS app. It has a UIImageView and some UILabels in it. I can get the cell to be focused by implementing the UIFocusEnvironment protocol without any issue, but I can't figure out how to give my custom cell the focused appearance. (Elevation and responding to user movement on the touchpad).

I'm aware of UIImageView's adjustsImageWhenAncestorFocused property, but that only elevates the image in my cell, not the entire cell.

Is there a way to make tvOS apply the (seemingly) standard focus appearance/behavior to my custom view or do I have to do it all manually?

Thanks in advance.

like image 983
Arash Payan Avatar asked Sep 21 '15 16:09

Arash Payan


1 Answers

Update in tvOS 12.0+: Check out new classes Apple has provided in TVUIKit! You can now make custom views that have this focusing behavior!

————

I asked the same question on the Apple developer forums. Apple staff answered:

For custom views you'll have to implement the focus appearance yourself. In the focus update method you can do things like apply a transform and use the UIMotionAffect API.

- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator {
    if (context.nextFocusedView == self) {
       // handle focus appearance changes
    }
    else {
       // handle unfocused appearance changes
    }
}

I think it'd be pretty helpful to make a UIView extension to be able to apply the same behavior to any custom view.

Maybe they'd like for us to implement more interesting ways to display focus to the user? That'd be a good reason to enable this easily only for UIImageView (Not to mention that this behavior also adds simulated light over the UIImageView, which is beautiful, but maybe only makes sense for images).

like image 51
sahandnayebaziz Avatar answered Sep 29 '22 08:09

sahandnayebaziz