为什么在 SwiftUI 中应用自定义 ButtonStyle 后我的箭头快捷键不起作用?
Why my arrow shortcuts don't work after I apply a custom ButtonStyle in SwiftUI?
专为 macOS 12 构建。在我使用自定义 ButtonStyle
后,如下所示。箭头 KeyboardShortcut
不起作用,但其他快捷方式如 space
或 return
或直接单击可以正常工作。
// CustomButtonStyle.swift
struct PlayerButton: ButtonStyle {
@State private var onHover: Bool = false
@State private var isPressed: Bool = false
var foreground: Color {
if isPressed {
return .primary
} else {
return .secondary
}
}
var background: AnyShapeStyle {
if isPressed {
return AnyShapeStyle(.ultraThickMaterial)
} else if onHover {
return AnyShapeStyle(.ultraThinMaterial)
} else {
return AnyShapeStyle(.background)
}
}
func makeBody(configuration: Configuration) -> some View {
configuration.label
.foregroundColor(foreground)
.frame(width: 32, height: 28, alignment: .center)
.background(background)
.cornerRadius(5)
.onHover { onHover in
self.onHover = onHover
}
.onChange(of: configuration.isPressed) { isPressed in
self.isPressed = isPressed
}
}
}
// ContenView.swift
struct ContentView: View {
var body: some View {
// Work fine
Button("Button 1") {
print("1")
}
.buttonStyle(PlayerButton())
.keyboardShortcut(.space, modifiers: [])
// Work fine
Button("Button 2") {
print("2")
}
.keyboardShortcut(.upArrow, modifiers: [])
//Doesn't work
Button("Button 3") {
print("3")
}
.buttonStyle(PlayerButton())
.keyboardShortcut(.downArrow, modifiers: [])
}
}
看起来像是一个 SwiftUI 错误。这是可能的安全解决方法(使用 Xcode 13.3 / macOS 12.3.1 测试)
这是主要部分:
ZStack {
Button("", action: button3action)
.opacity(0)
.keyboardShortcut(.downArrow, modifiers: [])
Button("Button 3", action: button3action)
.buttonStyle(PlayerButton())
}
// ...
func button3action() {
print("3")
}
专为 macOS 12 构建。在我使用自定义 ButtonStyle
后,如下所示。箭头 KeyboardShortcut
不起作用,但其他快捷方式如 space
或 return
或直接单击可以正常工作。
// CustomButtonStyle.swift
struct PlayerButton: ButtonStyle {
@State private var onHover: Bool = false
@State private var isPressed: Bool = false
var foreground: Color {
if isPressed {
return .primary
} else {
return .secondary
}
}
var background: AnyShapeStyle {
if isPressed {
return AnyShapeStyle(.ultraThickMaterial)
} else if onHover {
return AnyShapeStyle(.ultraThinMaterial)
} else {
return AnyShapeStyle(.background)
}
}
func makeBody(configuration: Configuration) -> some View {
configuration.label
.foregroundColor(foreground)
.frame(width: 32, height: 28, alignment: .center)
.background(background)
.cornerRadius(5)
.onHover { onHover in
self.onHover = onHover
}
.onChange(of: configuration.isPressed) { isPressed in
self.isPressed = isPressed
}
}
}
// ContenView.swift
struct ContentView: View {
var body: some View {
// Work fine
Button("Button 1") {
print("1")
}
.buttonStyle(PlayerButton())
.keyboardShortcut(.space, modifiers: [])
// Work fine
Button("Button 2") {
print("2")
}
.keyboardShortcut(.upArrow, modifiers: [])
//Doesn't work
Button("Button 3") {
print("3")
}
.buttonStyle(PlayerButton())
.keyboardShortcut(.downArrow, modifiers: [])
}
}
看起来像是一个 SwiftUI 错误。这是可能的安全解决方法(使用 Xcode 13.3 / macOS 12.3.1 测试)
这是主要部分:
ZStack {
Button("", action: button3action)
.opacity(0)
.keyboardShortcut(.downArrow, modifiers: [])
Button("Button 3", action: button3action)
.buttonStyle(PlayerButton())
}
// ...
func button3action() {
print("3")
}