2016-11-28 47 views
0

我有一個有兩個按鈕的UIView。在MyView類我有這樣的代碼:tvOS:焦點不能正確移動

-(BOOL) canBecomeFocused { 
    return YES; 
} 

-(NSArray<id<UIFocusEnvironment>> *)preferredFocusEnvironments { 
    return @[_editButton, _addButton]; 
} 

-(IBAction) editTapped:(id) sender { 
    BOOL editing = !tableViewController.editing; 
    [_editButton setTitle:editing ? @"Done" : @"Edit" forState:UIControlStateNormal]; 
    _addButton.hidden = !editing; 
    [tableViewController setEditing:editing animated:YES]; 

}

的基本思想是,用戶可以移動焦點到編輯按鈕,這樣就可以使添加按鈕出現。

問題出現了,因爲每次點擊編輯按鈕,焦點都會轉移到表格視圖。我實際上喜歡它移動到添加按鈕。我也希望它在編輯時停用,編輯按鈕保持焦點。但是它又一次轉移到桌面視圖。

所以我試了上面的代碼。這可以在焦點移動到視圖和按鈕上。但一旦它在那裏,我無法讓它移動到其他地方。

我讀過的一切都說只是覆蓋preferredFocusEnvironments但到目前爲止我還沒有能夠得到這個工作。焦點繼續按住按鈕,然後拒絕在其他地方移動。

任何想法?

+0

這證明確實很困難。我正在嘗試使用'UIFocusGuide',但它看起來像從按鈕向下移動到桌面視圖是系統生成的移動,並忽略了我的焦點指南。即使我坐在他們之間。哎呀。 ! – drekka

回答

0

您可以通過以下邏輯覆蓋preferredFocusEnviromnets:

-(NSArray<id<UIFocusEnvironment>> *)preferredFocusEnvironments { 
    if (condition) { 
     return @[_editButton]; 
    } 
    else { 
     return @[_addButton]; 
    } 
} 

設置後,您可以撥打

[_myView setNeedsFocusUpdate]; 
[_myView updateFocusIfNeeded]; 

的條件可以是BOOL condition = tableViewController.editing;或SG這樣。

如果現在可以使用,可以延遲調用(0.1秒左右)。