簡體   English   中英

如何在按鈕焦點上執行tvOS中的動作事件

[英]How to perform an action event in tvOS on button focus

我想在我的按鈕處於焦點時執行某些操作,而不是在tvOS中手動點擊它。

我有四個UIButtons水平排列,當用戶只關注4個按鈕中的一個時,我將展示一個包含一些信息的UIView。

如何在不需要單擊按鈕的情況下執行操作?

您可以在其中一個按鈕成為焦點視圖時執行操作。

您可以為每個按鈕分配一個標記,並使用聚焦按鈕的標記來確定要采取的操作。 為了便於閱讀,您可以為每個標記定義一個包含值的enum

enum FocusedButtonTag: Int {
    case First // Substitute with names that correspond to button's title/action
    case Second
    case Third
    case Fourth
}

override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
    super.didUpdateFocusInContext(context, withAnimationCoordinator: coordinator)
    guard let button = UIScreen.mainScreen().focusedView as? UIButton else {
        return
    }
    // Update your UIView with the desired information based on the focused button
    switch button.tag {
        case FocusedButtonTag.First.rawValue:
            ... // first button's action
        case FocusedButtonTag.Second.rawValue:
            ... // second button's action
        case FocusedButtonTag.Third.rawValue:
            ... // third button's action
        case FocusedButtonTag.Fourth.rawValue:
            ... // fourth button's action
        default:
            break
    }
}

暫無
暫無

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

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