gpt4 book ai didi

swift - 如何在 tvOS 的 UIButton 上获得视差效果?

转载 作者:搜寻专家 更新时间:2023-11-01 05:37:25 24 4
gpt4 key购买 nike

我正在尝试在 UIView 中为 tvOS 应用制作一些 UIButtons,但无法让它们在模拟器中显示新的视差效果。我在 images.xcassets 中成功设置了一个名为“startReadingButton”的电 View 像堆栈,我的按钮随文件一起加载,但在模拟器中的 Remote 上四处滑动时它们不显示 Shiny 的视差效果。这是我加载 UIButton 的方式:

   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)

}

到目前为止,我已经尝试了几乎所有我能想到的变体来找到解决方案,比如将按钮类型设置为 .Custom 和 .System,在 UIButton 的不同参数中设置图像等。但是我无法得到按钮进入视差模式并在聚焦时四处移动。

有人知道我需要做什么才能获得所需的视差效果吗?

最佳答案

解决了我的问题-

UIButtonsetImage 属性中为 UIControlState.Focused 状态设置 LSR 或 Apple TV 图像堆栈。

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 图像堆栈。

关于swift - 如何在 tvOS 的 UIButton 上获得视差效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35282354/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com