How to get Parallax Effect on UIButton in tvOS?

时光总嘲笑我的痴心妄想 提交于 2019-12-06 07:13:07

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!