我正在尝试在 tvOS 应用程序的UIView
中制作一些UIButtons
,但无法让他们在模拟器中显示新的视差效果。我成功地在images.xcassets
中设置了一个名为"startReadingButton"的电视图像堆栈,我的按钮随文件一起加载,但是在模拟器中的遥控器上滑动时,它们没有显示闪亮的视差效果。以下是我加载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)
}
到目前为止,我已经尝试了几乎所有我能想到的变体来找到解决方案,例如将按钮类型设置为两者。自定义和 .系统,在UIButton的不同参数中设置图像等。但是,我无法让按钮进入视差模式并在聚焦时四处移动。
有谁知道我需要做什么才能获得所需的视差效果?
解决了我的问题 -
在UIButton
的setImage
属性中将 LSR 或 Apple TV 图像堆栈设置为UIControlState.Focused
状态。
在UIButton
上设置imageView?.clipsToBounds = false
。
在UIButton
上设置newButton.imageView?.adjustsImageWhenAncestorFocused = true
。
我有以下代码按预期工作:
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"是我的images.xcassets目录中的Apple TV图像堆栈。