Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an LSR image on a UIButton

I am creating a tvOS app and I want to use parallax images on a couple of buttons. From the docs:

To incorporate parallax images in your app:

  1. Create a UIImage object.
  2. You load the image differently depending on whether the image is included in your app bundle or whether you have downloaded the image.
    • Bundle—Load images using imageNamed:.
    • Downloaded file—Load images using imageWithContentsOfFile:.
  3. Create a new UIImageView object using the loaded images.
  4. If the UIImageView is part of another view, set adjustsImageWhenAncestorFocused to YES on the UIImageView.

I know it says UIImageView there, but I was hoping to make the same effect happen on a UIButton, like the home screen app icons.

I've created the artwork, made a stack in the asset catalog, and loaded the image with imageNamed:, but the UIButton does not behave like a parallax image. It does not sway around like the home-screen icons do. It just looks like a flat image.

Is there something else I have to enable in order for the UIButton to behave like the home screen app icons?

UIButton* quitGame = [[UIButton alloc] initWithFrame:rectWithNewX(playAgain.frame, 985)];
[quitGame setImage:[UIImage imageNamed:@"quit.lsr"] forState:UIControlStateNormal];
[quitGame setAdjustsImageWhenHighlighted:YES];
fadeIn(quitGame, self.view, 0.5);
like image 231
Liftoff Avatar asked Mar 15 '23 16:03

Liftoff


1 Answers

As of right now, this is not possible with just UIButtons, however, I did find a workaround.

Instead, create a UIImageView that will fill the size of the UIButton and make it a subview of the UIButton. Then call the adjustsImageWhenAncestorFocused: and set it to true. Voila!

UIButton* playAgain = [[UIButton alloc] initWithFrame:CGRectMake(centerX(650, self.view), sh() - 250, 300, 180)];
[playAgain setAdjustsImageWhenHighlighted:YES];
[playAgain addTarget:self action:@selector(playAgain) forControlEvents:UIControlEventPrimaryActionTriggered];
fadeIn(playAgain, self.view, 0.5);

UIImageView* playAgainIGV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 180)];
[playAgainIGV setImage:[UIImage imageNamed:@"playagain.lsr"]];
[playAgainIGV setAdjustsImageWhenAncestorFocused:YES];
[playAgain addSubview:playAgainIGV];
like image 104
Liftoff Avatar answered Mar 27 '23 19:03

Liftoff