使用 UIFocusGuide 在 tvOS 中进行对角线聚焦
Diagonal focussing in tvOS using UIFocusGuide
我认为有 4 个 UIButtons
:
四个都是UIButtons
,左边两个UIButtons
,右边两个。当我 select 向下箭头时,焦点从顶部按钮移动到底部按钮。当我按下向下箭头和向上箭头时,我希望焦点从上到下浏览所有四个按钮,然后再从下到上浏览。
我还想在突出显示时更改 UIBUtton
的背景颜色(当前默认为白色)。
我尝试了下面的代码来导航焦点,但我无法得到我想要的。
let focusGuide = UIFocusGuide()
view.addLayoutGuide(focusGuide!)
focusGuide!.rightAnchor.constraintEqualToAnchor(self.btnPlay.rightAnchor).active = true
focusGuide!.topAnchor.constraintEqualToAnchor(self.switchFirst.topAnchor).active = true
focusGuide!.widthAnchor.constraintEqualToAnchor(self.btnPlay.widthAnchor).active = true
focusGuide!.heightAnchor.constraintEqualToAnchor(self.switchFirst.heightAnchor).active = true
focusGuide!.preferredFocusedView = self.switchFirst
在 viewdidLoad 和
didUpdateFocusInContext 中的以下代码
guard let nextFocusedView = context.nextFocusedView else { return }
switch nextFocusedView {
case self.switchFirst:
self.focusGuide!.preferredFocusedView = self.btnPlay
case self.btnPlay:
self.focusGuide!.preferredFocusedView = self.switchFirst
case self.switchAudioType:
self.focusGuide!.preferredFocusedView = self.btnAdd
case self.btnAddToWishList:
self.focusGuide!.preferredFocusedView = self.switchSecond
好吧,一种简单的方法是使用 4 种不同的焦点指南。我已经包含了一个 非常 的基本图像,说明它们将在哪里。焦点指南放置:
// [1] in viewDidLoad
let firstFocusGuide = UIFocusGuide()
view.addLayoutGuide(firstFocusGuide)
firstFocusGuide.leftAnchor.constraintEqualToAnchor(self.btnPlay.leftAnchor).active = true
firstFocusGuide.topAnchor.constraintEqualToAnchor(self.btnPlay.bottomAnchor).active = true
firstFocusGuide.heightAnchor.constraintEqualToAnchor(self.btnPlay.heightAnchor).active = true
firstFocusGuide.widthAnchor.constraintEqualToAnchor(self.switchFirst.widthAnchor).active = true
firstFocusGuide.preferredFocusedView = self.switchFirst
let secondFocusGuide = UIFocusGuide()
view.addLayoutGuide(secondFocusGuide)
secondFocusGuide.rightAnchor.constraintEqualToAnchor(self.switchFirst.rightAnchor).active = true
secondFocusGuide.bottomAnchor.constraintEqualToAnchor(self.switchFirst.topAnchor).active = true
secondFocusGuide.heightAnchor.constraintEqualToAnchor(self.switchFirst.heightAnchor).active = true
secondFocusGuide.widthAnchor.constraintEqualToAnchor(self.switchFirst.widthAnchor).active = true
secondFocusGuide.preferredFocusedView = self.btnPlay
let thirdFocusGuide = UIFocusGuide()
view.addLayoutGuide(thirdFocusGuide)
thirdFocusGuide.leftAnchor.constraintEqualToAnchor(self.btnAdd.leftAnchor).active = true
thirdFocusGuide.bottomAnchor.constraintEqualToAnchor(self.btnAdd.topAnchor).active = true
thirdFocusGuide.heightAnchor.constraintEqualToAnchor(self.btnAdd.heightAnchor).active = true
thirdFocusGuide.widthAnchor.constraintEqualToAnchor(self.switchSecond.widthAnchor).active = true
thirdFocusGuide.preferredFocusedView = self.switchSecond
let fourthFocusGuide = UIFocusGuide()
view.addLayoutGuide(fourthFocusGuide)
fourthFocusGuide.rightAnchor.constraintEqualToAnchor(self.switchSecond.rightAnchor).active = true
fourthFocusGuide.topAnchor.constraintEqualToAnchor(self.switchSecond.bottomAnchor).active = true
fourthFocusGuide.heightAnchor.constraintEqualToAnchor(self.switchSecond.heightAnchor).active = true
fourthFocusGuide.widthAnchor.constraintEqualToAnchor(self.switchSecond.widthAnchor).active = true
fourthFocusGuide.preferredFocusedView = self.btnAdd
请记住,这只允许在四个按钮之间进行 Up
和 Down
移动。更不用说它有点乏味了...希望对您有所帮助!
我认为有 4 个 UIButtons
:
四个都是UIButtons
,左边两个UIButtons
,右边两个。当我 select 向下箭头时,焦点从顶部按钮移动到底部按钮。当我按下向下箭头和向上箭头时,我希望焦点从上到下浏览所有四个按钮,然后再从下到上浏览。
我还想在突出显示时更改 UIBUtton
的背景颜色(当前默认为白色)。
我尝试了下面的代码来导航焦点,但我无法得到我想要的。
let focusGuide = UIFocusGuide()
view.addLayoutGuide(focusGuide!)
focusGuide!.rightAnchor.constraintEqualToAnchor(self.btnPlay.rightAnchor).active = true
focusGuide!.topAnchor.constraintEqualToAnchor(self.switchFirst.topAnchor).active = true
focusGuide!.widthAnchor.constraintEqualToAnchor(self.btnPlay.widthAnchor).active = true
focusGuide!.heightAnchor.constraintEqualToAnchor(self.switchFirst.heightAnchor).active = true
focusGuide!.preferredFocusedView = self.switchFirst
在 viewdidLoad 和
didUpdateFocusInContext 中的以下代码
guard let nextFocusedView = context.nextFocusedView else { return }
switch nextFocusedView {
case self.switchFirst:
self.focusGuide!.preferredFocusedView = self.btnPlay
case self.btnPlay:
self.focusGuide!.preferredFocusedView = self.switchFirst
case self.switchAudioType:
self.focusGuide!.preferredFocusedView = self.btnAdd
case self.btnAddToWishList:
self.focusGuide!.preferredFocusedView = self.switchSecond
好吧,一种简单的方法是使用 4 种不同的焦点指南。我已经包含了一个 非常 的基本图像,说明它们将在哪里。焦点指南放置:
// [1] in viewDidLoad
let firstFocusGuide = UIFocusGuide()
view.addLayoutGuide(firstFocusGuide)
firstFocusGuide.leftAnchor.constraintEqualToAnchor(self.btnPlay.leftAnchor).active = true
firstFocusGuide.topAnchor.constraintEqualToAnchor(self.btnPlay.bottomAnchor).active = true
firstFocusGuide.heightAnchor.constraintEqualToAnchor(self.btnPlay.heightAnchor).active = true
firstFocusGuide.widthAnchor.constraintEqualToAnchor(self.switchFirst.widthAnchor).active = true
firstFocusGuide.preferredFocusedView = self.switchFirst
let secondFocusGuide = UIFocusGuide()
view.addLayoutGuide(secondFocusGuide)
secondFocusGuide.rightAnchor.constraintEqualToAnchor(self.switchFirst.rightAnchor).active = true
secondFocusGuide.bottomAnchor.constraintEqualToAnchor(self.switchFirst.topAnchor).active = true
secondFocusGuide.heightAnchor.constraintEqualToAnchor(self.switchFirst.heightAnchor).active = true
secondFocusGuide.widthAnchor.constraintEqualToAnchor(self.switchFirst.widthAnchor).active = true
secondFocusGuide.preferredFocusedView = self.btnPlay
let thirdFocusGuide = UIFocusGuide()
view.addLayoutGuide(thirdFocusGuide)
thirdFocusGuide.leftAnchor.constraintEqualToAnchor(self.btnAdd.leftAnchor).active = true
thirdFocusGuide.bottomAnchor.constraintEqualToAnchor(self.btnAdd.topAnchor).active = true
thirdFocusGuide.heightAnchor.constraintEqualToAnchor(self.btnAdd.heightAnchor).active = true
thirdFocusGuide.widthAnchor.constraintEqualToAnchor(self.switchSecond.widthAnchor).active = true
thirdFocusGuide.preferredFocusedView = self.switchSecond
let fourthFocusGuide = UIFocusGuide()
view.addLayoutGuide(fourthFocusGuide)
fourthFocusGuide.rightAnchor.constraintEqualToAnchor(self.switchSecond.rightAnchor).active = true
fourthFocusGuide.topAnchor.constraintEqualToAnchor(self.switchSecond.bottomAnchor).active = true
fourthFocusGuide.heightAnchor.constraintEqualToAnchor(self.switchSecond.heightAnchor).active = true
fourthFocusGuide.widthAnchor.constraintEqualToAnchor(self.switchSecond.widthAnchor).active = true
fourthFocusGuide.preferredFocusedView = self.btnAdd
请记住,这只允许在四个按钮之间进行 Up
和 Down
移动。更不用说它有点乏味了...希望对您有所帮助!