SwiftUI tvOS 按钮/NavigationLink .focusable() [英] SwiftUI tvOS Button/NavigationLink .focusable()

查看:25
本文介绍了SwiftUI tvOS 按钮/NavigationLink .focusable()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个直接的跨平台 SwiftUI 项目.我在 tvOS 上使用 NavigationLink 时遇到了困难.

I have a straight forward SwiftUI project that's cross-platform. And I'm having a hard time working with NavigationLink on tvOS.

(以下均为伪代码,可能有错别字)

(Everything below is pseudocode, could be typos)

以这个为例,这很好用:

Take this for instance, this works great:

struct ContentView: View {
  var body: some View {
    NavigationView {
      ScrollView(.horizontal) {
        HStack {
          ForEach(items) { item in
            NavigationLink(destination: DetailView(item: item)) {
              Card(item: item)
            }
          }
        }
      }
    }
  }
}

我的一个要求是,当您在列表中滑动时,背景图像会随着焦点项目而变化.

A requirement I have is when you are swiping through the list, the background image changes with the focused item.

现在,我想要做的就是这个.但是在 NavigationLink 上覆盖 .focusable() 似乎禁用了它的点击动作.

Now, what I want to do is this. But overriding .focusable() on NavigationLink seems to disable it's tapping action.

struct ContentView: View {
  // Store Focused Item
  @State private var currentItem: Item?

  var body: some View {
    NavigationView {
      ScrollView(.horizontal) {
        HStack {
          ForEach(items) { item in
            NavigationLink(destination: DetailView(item: item)) {
              Card(item: item)
            }
            // Set which item is highlighted
            .focusable(true) { isFocused in
              currentItem = item
            }
          }
        }
      }
    }
  }
}

到目前为止我尝试过的:

我想我可以聪明一点,使用动态 NavigationLink,并制作一个可聚焦的按钮,为我设置标签.这有相同的结果:(

I thought I could be clever and use a dynamic NavigationLink, and make a focusable button that set a tag for me. This had the same result :(

struct ContentView: View {
  @State private var currentItem: Item?
  // Store Selection Tag
  @State private var selection: String? = nil

  var body: some View {
    // ...

    NavigationLink(destination: DetailView(item: item), tag: item.title, selection: $selection) {
      EmptyView()
    }
  
    Button(action: { selection = item.title }) {
      Card(item: item)
    }
    .buttonStyle(PlainButtonStyle())
    .focusable(true) { isFocused in
      currentItem = item
    }

    // ...
  }
}

** 问题 **

  1. 可以在 .focusable() 配置它仍然调用操作的地方吗?
  2. 有没有办法报告当前关注的项目?
  1. Can .focusable() be configured where it still calls the action?
  2. Is there any way of reported the current focused item?

感谢您对此的任何帮助.这看起来很简单,但我已经快没救了.

Thanks for any help with this. This seems so straight forward, but I'm at the end of my rope.

推荐答案

我找到了答案.那是因为在引入 Focusable 时,它​​将打破当前的焦点引擎状态.为了更好地控制焦点行为,请不要使用focusable,请使用.onchange 作为焦点更改触发事件.

I found the answer. That is because when Focusable was introduced, it will break the current focus engine status. to better control the Focus behavior, don't use focusable, please use .onchange for the focus change triggered event.

这篇关于SwiftUI tvOS 按钮/NavigationLink .focusable()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆