使用UIFocusGuide在tvOS中对角线聚焦

时间:2016-01-05 16:58:07

标签: swift tvos

我的观点中有4个UIButtons

enter image description here

所有四个都是UIButtons,左侧有两个UIButtons,右侧有两个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 的背景颜色(默认情况下目前为白色)。

我尝试使用以下代码来导航焦点,但我无法获得我想要的内容。

    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

在viewdidLoad和

下面是didUpdateFocusInContext中的代码

{{1}}

1 个答案:

答案 0 :(得分:3)

一个简单的方法是使用4种不同的焦点指南。我已经包含了非常的基本图像。焦点指南安置:

enter image description here

let lens : Lens<Person, Address> =
Lens(
    extract: {(p:Person)->Address in
        return p.address_}, // here's the error
    create: {Person($0.name_,
        Address(street_: $1, city_: $0.address_.city_))})

请注意,这只会允许// [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 &amp;四个按钮之间的Up个动作。更不用说它有点乏味......希望有所帮助!