gpt4 book ai didi

swift - 如何在 SwiftUI 中将单击事件添加到整个 View

转载 作者:行者123 更新时间:2023-12-03 21:58:56 25 4
gpt4 key购买 nike

我正在开发 SwiftUI 测试应用程序,并在此处添加了我的自定义下拉菜单。
下拉菜单工作正常,但当用户单击区域外的下拉菜单时,我想关闭下拉菜单。

这是我的下拉菜单。

import SwiftUI

var dropdownCornerRadius:CGFloat = 3.0
struct DropdownOption: Hashable {
public static func == (lhs: DropdownOption, rhs: DropdownOption) -> Bool {
return lhs.key == rhs.key
}

var key: String
var val: String
}

struct DropdownOptionElement: View {
var dropdownWidth:CGFloat = 150
var val: String
var key: String
@Binding var selectedKey: String
@Binding var shouldShowDropdown: Bool
@Binding var displayText: String

var body: some View {
Button(action: {
self.shouldShowDropdown = false
self.displayText = self.val
self.selectedKey = self.key
}) {
VStack {
Text(self.val)
Divider()
}

}.frame(width: dropdownWidth, height: 30)
.padding(.top, 15).background(Color.gray)

}
}

struct Dropdown: View {
var dropdownWidth:CGFloat = 150
var options: [DropdownOption]
@Binding var selectedKey: String
@Binding var shouldShowDropdown: Bool
@Binding var displayText: String
var body: some View {
VStack(alignment: .leading, spacing: 0) {
ForEach(self.options, id: \.self) { option in
DropdownOptionElement(dropdownWidth: self.dropdownWidth, val: option.val, key: option.key, selectedKey: self.$selectedKey, shouldShowDropdown: self.$shouldShowDropdown, displayText: self.$displayText)
}
}

.background(Color.white)
.cornerRadius(dropdownCornerRadius)
.overlay(
RoundedRectangle(cornerRadius: dropdownCornerRadius)
.stroke(Color.primary, lineWidth: 1)
)
}
}

struct DropdownButton: View {
var dropdownWidth:CGFloat = 300
@State var shouldShowDropdown = false
@State var displayText: String
@Binding var selectedKey: String
var options: [DropdownOption]

let buttonHeight: CGFloat = 30
var body: some View {
Button(action: {
self.shouldShowDropdown.toggle()
}) {
HStack {
Text(displayText)
Spacer()
Image(systemName: self.shouldShowDropdown ? "chevron.up" : "chevron.down")
}
}
.padding(.horizontal)
.cornerRadius(dropdownCornerRadius)
.frame(width: self.dropdownWidth, height: self.buttonHeight)
.overlay(
RoundedRectangle(cornerRadius: dropdownCornerRadius)
.stroke(Color.primary, lineWidth: 1)
)
.overlay(
VStack {
if self.shouldShowDropdown {
Spacer(minLength: buttonHeight)
Dropdown(dropdownWidth: dropdownWidth, options: self.options, selectedKey: self.$selectedKey, shouldShowDropdown: $shouldShowDropdown, displayText: $displayText)
}
}, alignment: .topLeading
)
.background(
RoundedRectangle(cornerRadius: dropdownCornerRadius).fill(Color.white)
)
}
}



struct DropdownButton_Previews: PreviewProvider {
static let options = [
DropdownOption(key: "week", val: "This week"), DropdownOption(key: "month", val: "This month"), DropdownOption(key: "year", val: "This year")
]

static var previews: some View {
Group {
VStack(alignment: .leading) {
DropdownButton(displayText: "This month", selectedKey: .constant("Test"), options: options)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.green)
.foregroundColor(Color.primary)

VStack(alignment: .leading) {

DropdownButton(shouldShowDropdown: true, displayText: "This month", selectedKey: .constant("Test"), options: options)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.green)
.foregroundColor(Color.primary)
}
}
}

我想我可以通过向全身 View 添加点击事件并将下拉显示状态标志变量设置为 false 来实现这一点。
但我不确定如何将点击事件添加到整个 View 。
任何人都可以帮我解决这个问题吗?
谢谢。

最佳答案

您可以在窗口中尝试以下操作 ContentView

struct ContentView: View {
var body: some View {
GeometryReader { gp in // << consumes all safe space
// all content here
}
.onTapGesture {
// change state closing any dropdown here
}
}
// .edgesIgnoringSafeArea(.all) // uncomment if needed entire screen
}

关于swift - 如何在 SwiftUI 中将单击事件添加到整个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60707286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com