当颜色清除 SwiftUI 时,点击操作不起作用
Tap Action not working when Color is clear SwiftUI
当我的 foregroundColor
是 clear
时,我的 tapAction
无法识别点击。当我删除颜色时它工作正常。
这是我的代码:
ZStack {
RoundedRectangle(cornerRadius: 0)
.foregroundColor(Color.clear)
.frame(width: showMenu ? UIScreen.main.bounds.width : 0)
.tapAction {
self.showMenu.toggle()
}
RoundedRectangle(cornerRadius: 5)
.foregroundColor(Color.green)
.shadow(radius: 5, y: 2)
.padding(.trailing, 50)
.frame(width: showMenu ? UIScreen.main.bounds.width : 0)
}
.edgesIgnoringSafeArea(.top)
我还发现用 Color.clear
填充的形状不会生成可点击区域。
这里有两个解决方法:
使用Color.black.opacity(0.0001)
(甚至在 10-bits-per-channel 显示器上)。这会生成一种非常透明的颜色,它不会影响您的外观,并生成一个可点击的区域来填充其框架。我不知道 SwiftUI 是否足够聪明,可以跳过渲染颜色,所以我不知道它是否会影响性能。
使用GeometryReader
获取帧大小,然后使用contentShape
生成可点击区域:
GeometryReader { proxy in
Color.clear.contentShape(Path(CGRect(origin: .zero, size: proxy.size)))
}
正确的方法是在视图上使用.contentShape(Rectangle())
。
本教程中描述:
control-the-tappable-area-of-a-view 作者:Paul Hudson @twostraws
VStack {
Image("Some Image").resizable().frame(width: 50, height: 50)
Spacer().frame(height: 50)
Text("Some Text")
}
.contentShape(Rectangle())
.onTapGesture {
print("Do Something")
}
计算器
这是组件
struct InvisibleButton: View {
let action: (() -> Void)?
var body: some View {
Color.clear
.contentShape(Rectangle())
.onTapGesture {
action?()
}
}
}
用法:将您的视图和 InbisibleButton 放入 ZStack
ZStack {
**yourView()**
InvisibleButton {
print("Invisible button tapped")
}
}
你也可以做一个修改器来简化使用:
struct InvisibleButtonModifier: ViewModifier {
let action: (() -> Void)?
func body(content: Content) -> some View {
ZStack {
content
InvisibleButton(action: action)
}
}
}
**yourView()**
.modifier(InvisibleButtonModifier {
print("Invisible button tapped")
})
但是,如果您的 SwiftUI 视图有一个 UIKit 视图作为子视图,您将必须设置 Color.gray.opacity(0.0001) 才能忽略 UIView 的触摸
foregroundColor
是 clear
时,我的 tapAction
无法识别点击。当我删除颜色时它工作正常。
这是我的代码:
ZStack {
RoundedRectangle(cornerRadius: 0)
.foregroundColor(Color.clear)
.frame(width: showMenu ? UIScreen.main.bounds.width : 0)
.tapAction {
self.showMenu.toggle()
}
RoundedRectangle(cornerRadius: 5)
.foregroundColor(Color.green)
.shadow(radius: 5, y: 2)
.padding(.trailing, 50)
.frame(width: showMenu ? UIScreen.main.bounds.width : 0)
}
.edgesIgnoringSafeArea(.top)
我还发现用 Color.clear
填充的形状不会生成可点击区域。
这里有两个解决方法:
使用
Color.black.opacity(0.0001)
(甚至在 10-bits-per-channel 显示器上)。这会生成一种非常透明的颜色,它不会影响您的外观,并生成一个可点击的区域来填充其框架。我不知道 SwiftUI 是否足够聪明,可以跳过渲染颜色,所以我不知道它是否会影响性能。使用
GeometryReader
获取帧大小,然后使用contentShape
生成可点击区域:GeometryReader { proxy in Color.clear.contentShape(Path(CGRect(origin: .zero, size: proxy.size))) }
正确的方法是在视图上使用.contentShape(Rectangle())
。
本教程中描述:
control-the-tappable-area-of-a-view 作者:Paul Hudson @twostraws
VStack {
Image("Some Image").resizable().frame(width: 50, height: 50)
Spacer().frame(height: 50)
Text("Some Text")
}
.contentShape(Rectangle())
.onTapGesture {
print("Do Something")
}
这是组件
struct InvisibleButton: View {
let action: (() -> Void)?
var body: some View {
Color.clear
.contentShape(Rectangle())
.onTapGesture {
action?()
}
}
}
用法:将您的视图和 InbisibleButton 放入 ZStack
ZStack {
**yourView()**
InvisibleButton {
print("Invisible button tapped")
}
}
你也可以做一个修改器来简化使用:
struct InvisibleButtonModifier: ViewModifier {
let action: (() -> Void)?
func body(content: Content) -> some View {
ZStack {
content
InvisibleButton(action: action)
}
}
}
**yourView()**
.modifier(InvisibleButtonModifier {
print("Invisible button tapped")
})
但是,如果您的 SwiftUI 视图有一个 UIKit 视图作为子视图,您将必须设置 Color.gray.opacity(0.0001) 才能忽略 UIView 的触摸