13

Siri Remote のメニュー ボタンのクリックを処理するカスタム コードを実装しています。メニュー ボタンを押したときにフォーカスをカスタム メニューに変更するにはどうすればよいですか?

4

6 に答える 6

23

iOS 10 では、 preferredFocusedView の代わりに preferredFocusEnvironments を使用する必要があります。

以下の例で注目したい場合はbutton、以下のコードを参照してください。

@IBOutlet weak var button: UIButton!

override var preferredFocusEnvironments: [UIFocusEnvironment] {
    return [button]
}

override func viewDidLoad() {
    super.viewDidLoad()

    setNeedsFocusUpdate()
    updateFocusIfNeeded()
}
于 2016-11-18T10:11:33.450 に答える
19

最後に自分でそれを理解しました。またはのpreferredFocusedViewプロパティをオーバーライドする必要があります。UIViewUIViewController

Swift では、次のように動作します。

func myClickHandler() {
    someCondition = true
    self.setNeedsFocusUpdate()
    self.updateFocusIfNeeded()
    someCondition = false
}

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

Objective-C でゲッターをオーバーライドする方法をよく思い出せないので、誰かが投稿したい場合は、回答を編集します。

于 2015-09-24T15:04:11.573 に答える
16

上記の 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;
    }
}

次に、コードで使用します

viewToFocus = myUIView;
于 2015-11-25T16:51:07.167 に答える
6

これが目標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またはビューでは、次のように表示されました:

    UIFocusGuide *focusGuide = [[UIFocusGuide alloc]init];
    focusGuide.preferredFocusedView = [self preferredFocusedView];
    [self.view addLayoutGuide:focusGuide];

最初の起動時に実行したい場合

于 2015-10-23T18:19:54.150 に答える
0

これは、Swift 2 の素敵な小さなコピー/貼り付けスニペットです。

var myPreferredFocusedView: UIView?
override var preferredFocusedView: UIView? {
    return myPreferredFocusedView
}
func updateFocus(to view: UIView) {
    myPreferredFocusedView = napDoneView
    setNeedsFocusUpdate()
    updateFocusIfNeeded()
}

次のように使用します。

updateFocus(to: someAwesomeView)
于 2016-10-10T23:13:47.127 に答える