私はSiri Remote上のMenuボタンのクリックを処理するカスタムコードを実装しています。 メニューボタンを押したときにフォーカスを強制的にカスタムメニューに変更するにはどうすればよいですか?tvOSの特定のビューにフォーカスを強制的に変更するにはどうすればよいですか?
答えて
最後に自分で考え出しました。 UIView
またはUIViewController
のpreferredFoucsedView
プロパティを上書きする必要があります。
迅速で、それはこのように書きます:
func myClickHandler() {
someCondition = true
self.setNeedsFocusUpdate()
self.updateFocusIfNeeded()
someCondition = false
}
override weak var preferredFocusedView: UIView? {
if someCondition {
return theViewYouWant
} else {
return defaultView
}
}
私はかなり誰かが私は答えを編集しますことを投稿したいので、もしObjective-Cでゲッターをオーバーライドする方法を覚えていないことができます。あなたはそれが最初にここ
はC
- (UIView *)preferredFocusedView
{
if (someCondition) {
// this is if your menu is a tableview
NSIndexPath *ip = [NSIndexPath indexPathForRow:2 inSection:0];
UITableViewCell * cell = [self.categoryTableView cellForRowAtIndexPath:ip];
return cell;
}
return self.view.preferredFocusedView;
}
あなたのviewDidLoadで
またはビューがこのような何かを表示された目的であります上記のSlaytersの答えに基づく別の実装です。その少し優雅な私は条件付きブール値を使用するよりも思う。
は その後var viewToFocus: UIView? = nil {
didSet {
if viewToFocus != nil {
self.setNeedsFocusUpdate();
self.updateFocusIfNeeded();
}
}
}
override weak var preferredFocusedView: UIView? {
if viewToFocus != nil {
return viewToFocus;
} else {
return super.preferredFocusedView;
}
}
はあなたのコードでそれを使用するために、あなたのViewControllerをでこれを入れて
viewToFocus = myUIView;
私はセルのためのnillを得て、私はtableviewの代わりにcollectionviewを使用しています!なぜそれがゼロであるかの任意のアイデア? –
を起動したときにそれを実行したい場合は
UIFocusGuide *focusGuide = [[UIFocusGuide alloc]init];
focusGuide.preferredFocusedView = [self preferredFocusedView];
[self.view addLayoutGuide:focusGuide];
:ここ
これはiOS 10で廃止されました。preferredFocusEnvironmentsを使用した推奨 – nyxee
elu5ionの答え@ますが、Objective-Cの 最初の宣言で:
@property (nonatomic) UIView *preferredView;
これらの方法を設定します。
-(void)setPreferredView:(UIView *)preferredView{
if (preferredView != nil) {
_preferredView = nil;
UIFocusGuide *focusGuide = [[UIFocusGuide alloc]init];
[self.view addLayoutGuide:focusGuide];
focusGuide.preferredFocusedView = [self preferredFocusedView];
[self setNeedsFocusUpdate];
[self updateFocusIfNeeded];
}
_preferredView = preferredView;
}
- (UIView *)preferredFocusedView {
if (_preferredView) {
return _preferredView;
}
return self.view.preferredFocusedView;
}
は、ここでちょっといいスウィフト2コピー/ペースト抜粋です:
var myPreferredFocusedView: UIView?
override var preferredFocusedView: UIView? {
return myPreferredFocusedView
}
func updateFocus(to view: UIView) {
myPreferredFocusedView = napDoneView
setNeedsFocusUpdate()
updateFocusIfNeeded()
}
はこのようにそれを使用してください:
はupdateFocus(to: someAwesomeView)
IOS 10についてあなたの代わりにpreferredFocusedViewのpreferredFocusEnvironmentsを使用する必要があります。
以下の例では、btnEnglishに焦点を当てたい場合は、以下のコードを参照してください。
@IBOutlet weak var btnEnglish:UIButton!
override var preferredFocusEnvironments : [UIFocusEnvironment] {
//condition
return [btnEnglish]
}
override func viewDidLoad() {
super.viewDidLoad()
self.setNeedsFocusUpdate()
self.updateFocusIfNeeded()
}
'preferredFocusedView'が償却されているので、これは受け入れられた答えです。 –
ありがとうございます。私はこれをやってみようと考えていましたが、これらすべての "someConditions"を全面に置くのは面倒なようです。あなたがちょうどPrePreditedFocusedViewを設定することができればもっと簡単だろう... –
しかし、待ってください。フォーカスを別のViewControllerに変更したいとき、これはどのように機能しますか? –
私は同意します。うまくいけば彼らはそれを追加します。また、すべての 'someCondition'を避けるために、私はviewControllerに' viewToFocus' 'UIView'プロパティを追加しました。その後、getterで、そのビューがnilではないかどうかをチェックして返します。それ以外の場合は、デフォルトのビューを返します。 – Slayter