簡體   English   中英

如何將焦點移到tvos應用程序的collectionview單元中的按鈕上?

[英]How to move focus at button in collectionview cell in tvos application?

我正在研究Apple TV應用程序。 在我的應用程序中,我制作了一個具有收藏夾視圖的屏幕。 在那種情況下,我可以將焦點移到集合視圖單元格上,但是不能將焦點移到集合視圖單元格中的按鈕上,那么有人可以幫助我解決此問題嗎?

如果有人知道這個答案,請給我一個答案。

我可以通過在collectionViewCell子類中添加以下方法來解決此問題。

在CollectionViewCell子類中

override var preferredFocusEnvironments: [UIFocusEnvironment]{
    // Condition
}
override func shouldUpdateFocus(in context: UIFocusUpdateContext) -> Bool {
    // Condition
}
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
    // Condition
}

您可以在此鏈接中看到更多信息: 在此處輸入鏈接描述

我認為此頁面將指導您實現所需的內容。 https://developer.apple.com/library/content/documentation/General/Conceptual/AppleTV_PG/WorkingwiththeAppleTVRemote.html#//apple_ref/doc/uid/TP40015241-CH5-SW4

它很好地說明了焦點引擎如何決定哪個對象應該獲得下一個焦點。 下面是以上鏈接的逐步詳細說明

這是顯示如何確定焦點的示例:

  1. 焦點引擎向根窗口詢問其preferredFocusedView,它返回其根視圖控制器的preferredFocusedView對象。
  2. 根視圖控制器(一個選項卡視圖控制器)返回其選擇視圖控制器的preferredFocusedView對象。
  3. 選擇視圖控制器重寫其preferredFocusedView方法以返回特定的UIButton實例。
  4. UIButton實例返回self(默認值),並且是可聚焦的,因此焦點引擎將其選擇為下一個焦點視圖。

您必須重寫UIView或UIViewController的preferredFoucsedView屬性。

override weak var preferredFocusedView: UIView? {
    if someCondition {
        return theViewYouWant
    } else {
        return defaultView
    }
}

感謝Slayter的回答

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM