gpt4 book ai didi

ios - 在 Apple TV 上使用多个控件管理焦点

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:46 24 4
gpt4 key购买 nike

我的屏幕上有多个控件。右上角的 Collection View ,然后是左中心的按钮,除了按钮,我还有另一个 Collection View 。请引用附图enter image description here

我能够将焦点从按钮移动到底部 Collection View ,反之亦然。我已经创建了一个焦点指南,如下所示:


focusGuide.preferredFocusedView = self.btn
self.view.addLayoutGuide(self.focusGuide)

self.focusGuide.topAnchor.constraintEqualToAnchor(collectionViewHeader.topAnchor).active = true
self.focusGuide.bottomAnchor.constraintEqualToAnchor(collectionViewBottom.topAnchor).active = true
self.focusGuide.leadingAnchor.constraintEqualToAnchor(collectionViewBottom.leadingAnchor).active = true
self.focusGuide.widthAnchor.constraintEqualToAnchor(collectionViewBottom.widthAnchor).active = true

在 didUpdateFocusInContext: 中,我写了:


override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
super.didUpdateFocusInContext(context, withAnimationCoordinator: coordinator)

guard let nextFocusedView = context.nextFocusedView else { return }

if(nextFocusedView .isKindOfClass(bottomCell)) {
self.focusGuide.preferredFocusedView = self.btn
} else {
self.focusGuide.preferredFocusedView = self.collectionViewBottom
}
}

但是,我无法将焦点从按钮移动到顶级 Collection View 。为此,我可能需要多个焦点指南,但我不知道那里应该有什么。谁可以帮我这个事?

谢谢

最佳答案

我在 UI 的许多部分都遇到了类似的问题,因此最终定义了一个自定义 View 来创建更大的屏幕区域来表示可聚焦控件组。因为我设置了这些较大的区域来覆盖不包含控件的空白区域,所以它们会拦截焦点移动并将焦点转移到区域内的控件,而与焦点开始的原始控件的垂直或水平对齐无关。

这是自定义 View 。您可以通过在 IB 中放置一个或多个控件来使用它。

请注意,它具有其他功能,例如无需覆盖 preferredFocusView 即可将焦点强制到特定控件,但如果不需要它们,您可以删除它们)。

class FocusGroup : UIView
{
weak private var nextFocusView:UIView? = nil
weak var initialView:UIView? = nil
var captureFocus:Bool = false

func focusOnView(view:UIView, now:Bool=false)
{
if not(view.isDescendantOfView(self))
|| view === self
{ return }

nextFocusView = view
setNeedsFocusUpdate()
if now { updateFocusIfNeeded() }
}

func resetFocus(now now:Bool=false)
{
nextFocusView = nil
setNeedsFocusUpdate()
if now { updateFocusIfNeeded() }
}

override func canBecomeFocused() -> Bool
{
if nextFocusView != nil { return true }
if containsFocus { return false }
return firstFocusableSubView() != nil
}


func firstFocusableSubView() -> UIView?
{

return findSubview({
$0.canBecomeFocused()
&& $0.userInteractionEnabled
&& $0.visible
&& ( not($0 is UIButton)
|| ($0 as! UIButton).enabled )
})
}


override var preferredFocusedView: UIView?
{
if let viewToFocus = ( nextFocusView ?? initialView ) ?? firstFocusableSubView()
{
return viewToFocus
}
return nil
}

override func shouldUpdateFocusInContext(context: UIFocusUpdateContext) -> Bool
{
// when capturing focus, prevent navigation outside of grouped subviews
if captureFocus
&& containsFocus
&& context.previouslyFocusedView!.isDescendantOfView(self)
&& (
context.nextFocusedView == nil
|| context.nextFocusedView === self
|| not(context.nextFocusedView!.isDescendantOfView(self))
)
{ return false }

return true
}

override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator)
{
// give focus to specific view as requested
if nextFocusView != nil
{
if context.nextFocusedView === nextFocusView
|| not(nextFocusView!.canBecomeFocused())
{ nextFocusView = nil }
return
}
}

}

关于ios - 在 Apple TV 上使用多个控件管理焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37006755/

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