Using an LSR Image on a UIButton

I am creating a tvOS application and I want to use parallax images on several buttons. From the docs :

To include images in parallax in your application:

  • Create a UIImage object.
  • You download an image in different ways depending on whether the image is included in the application bundle or if the image is uploaded.
    • Downloading packages using imageNamed :.
    • Uploaded File - Download images using imageWithContentsOfFile :.
  • Create a new UIImageView object using the uploaded images.
  • If the UIImageView is part of another view, set the UIImageView parameter to the ImageWhenAncestorFocused parameter in YES.

I know what is written there UIImageView, but I was hoping to make the same effect on UIButtonas the application icons on the main screen.

I created a cover, made a stack in the asset catalog, and uploaded the image using imageNamed:, but UIButtondoes not behave like a parallax image. It does not act like the icons on the home screen do. It looks like a flat image.

Is there anything else I need to enable in order to UIButtonbehave like application icons on the main screen?

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);
+4
source share
3 answers

As of now, this is not possible with only UIButtons, however I found a workaround.

UIImageView, UIButton UIButton. adjustsImageWhenAncestorFocused: true. !

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];
+5

LSR.

LSR XCode → Apple TV Image Stack PNG, Interface Builder, :

-(void) setLsr:(UIButton *)button lsrNamed:(NSString *)lsr {
    [button setAdjustsImageWhenHighlighted:YES];

    UIImageView *biv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, button.frame.size.width, button.frame.size.height)];
    [biv setImage:[UIImage imageNamed:lsr]];
    [biv setAdjustsImageWhenAncestorFocused:YES];
    [button addSubview:biv];
}
+1

As for the david answer , I created a simple Swift method to create a parallax effect :

func createParallaxButton(button: UIButton, imageNamed: String) {
        button.adjustsImageWhenHighlighted = true
        let buttonBg = UIImageView(image: UIImage(named: imageNamed))
        buttonBg.adjustsImageWhenAncestorFocused = true
        buttonBg.frame = button.bounds
        button.addSubview(buttonBg)
    }
createParallaxButton(myButton, imageNamed: "myButtonimage.lsr")
+1
source

Source: https://habr.com/ru/post/1608607/


All Articles