Search code examples
swiftparallaxtvos

How to get Parallax Effect on UIButton in tvOS?


I'm trying to make some UIButtons in a UIView for a tvOS app but have been unable to get them to display the new parallax effect in the simulator. I successfully set up a TV image stack in images.xcassets called "startReadingButton" and my buttons load with the file, but they do not display the shiny parallax effect when swiping around on the remote in the simulator. Here is how I am loading my UIButtons:

   for button in 1...5 {

        let image = UIImage(named: "startReadingButton")

        let newButton = UIButton(type: .Custom)
        newButton.frame = buttonRects[button - 1]
        newButton.setBackgroundImage(image, forState: UIControlState.Focused)
        newButton.imageView?.image = image
        newButton.imageView?.adjustsImageWhenAncestorFocused = true
        newButton.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.PrimaryActionTriggered)
        newButton.tag = button
        newButton.canBecomeFocused()
        newButton.adjustsImageWhenHighlighted = true
        newButton.clipsToBounds = false
        self.addSubview(newButton)
        self.homeButtons.append(newButton)

    }

So far, I have tried almost every variation I could think of to find a solution, like setting the button type to both .Custom and .System, setting the image in different parameters of the UIButton, etc. However I cannot get the buttons to enter parallax mode and move around when they are focused.

Anyone know what I need to do to get the desired parallax effect?


Solution

  • Solved my problem -

    Set your LSR or Apple TV image stack in your UIButton's setImage property for the UIControlState.Focused state.

    Set the imageView?.clipsToBounds = false on the UIButton.

    Set newButton.imageView?.adjustsImageWhenAncestorFocused = true on the UIButton.

    I have got the following code to work as expected:

      for button in 1...3 {
    
            let buttonUnpressedTexture = UIImage(contentsOfFile:NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent("startReadingUnpressed.png"))!
            let buttonPressedTexture = UIImage(contentsOfFile:NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent("startReadingPressed.png"))!
    
            let newButton = UIButton(type: .Custom)
            newButton.frame = buttonRects[button - 1]
            newButton.setImage(buttonUnpressedTexture, forState: UIControlState.Normal)
            newButton.setImage(buttonPressedTexture, forState: UIControlState.Highlighted)
            newButton.setImage(UIImage(named: "StartReadingButton"), forState: UIControlState.Focused)
            newButton.imageView?.clipsToBounds = false
            newButton.imageView?.adjustsImageWhenAncestorFocused = true
            newButton.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.PrimaryActionTriggered)
            newButton.tag = button
            newButton.canBecomeFocused()
            self.addSubview(newButton)            
        }
    

    "StartReadingButton" is an Apple TV image stack in my images.xcassets catalog.