appletv如何对焦点变化事件做出反应
appletv how to react on focus change event
我试图在 AppleTV 上更改焦点更改按钮的文本颜色,这是我的代码(见下文)。
不幸的是,带有 if focus .. 的代码从未被调用。我做错了什么?
感谢您的帮助!
struct ContentView: View {
@State var textColor : Color = .white
var body: some View {
VStack {
Button(action: {
self.textColor = .black
}) {
Text("tap me")
}
.focusable(true) { (focus) in
if focus {
self.textColor = .blue
} else {
self.textColor = .green
}
}
Button(action: {
self.textColor = .black
}) {
Text("another tap me")
}
.focusable(true) { (focus) in
if focus {
self.textColor = .blue
} else {
self.textColor = .green
}
}
}
}
}
.focusable
为本质上不可聚焦的元素添加了功能,例如 Text
(或 Image
),但是 Button
是可聚焦的,所以没有发生。
您的示例的以下修改有效(使用 Xcode 11.2 测试):
var body: some View {
VStack {
Text("Focusable").foregroundColor(textColor)
.focusable(true) { (focus) in
if focus {
self.textColor = .blue
} else {
self.textColor = .green
}
}
Button(action: {
self.textColor = .black
}) {
Text("Button")
}
}
}
我试图在 AppleTV 上更改焦点更改按钮的文本颜色,这是我的代码(见下文)。
不幸的是,带有 if focus .. 的代码从未被调用。我做错了什么?
感谢您的帮助!
struct ContentView: View {
@State var textColor : Color = .white
var body: some View {
VStack {
Button(action: {
self.textColor = .black
}) {
Text("tap me")
}
.focusable(true) { (focus) in
if focus {
self.textColor = .blue
} else {
self.textColor = .green
}
}
Button(action: {
self.textColor = .black
}) {
Text("another tap me")
}
.focusable(true) { (focus) in
if focus {
self.textColor = .blue
} else {
self.textColor = .green
}
}
}
}
}
.focusable
为本质上不可聚焦的元素添加了功能,例如 Text
(或 Image
),但是 Button
是可聚焦的,所以没有发生。
您的示例的以下修改有效(使用 Xcode 11.2 测试):
var body: some View {
VStack {
Text("Focusable").foregroundColor(textColor)
.focusable(true) { (focus) in
if focus {
self.textColor = .blue
} else {
self.textColor = .green
}
}
Button(action: {
self.textColor = .black
}) {
Text("Button")
}
}
}