Ios ONTAPPORATE禁用我的选取器和分段点击

Ios ONTAPPORATE禁用我的选取器和分段点击,ios,swiftui,gesture,picker,Ios,Swiftui,Gesture,Picker,我正在使用SwiftUI创建一个表单。在我的表单中,我有一个日期选择器,文本字段,和一个分段选择器样式 我的TextField正在使用.decimalPad,我正试图找到在完成键入时关闭键盘的最佳方法 我已尝试添加.ontapsignate,但它阻止我使用任何选择器。当我点击它们时,什么也没发生 以下是我试图做的: struct ContentView: View { @State var date = Date() @State var price = "" @Sta

我正在使用
SwiftUI
创建一个
表单。在我的
表单
中,我有一个
日期选择器
文本字段
,和一个
分段选择器样式

我的
TextField
正在使用
.decimalPad
,我正试图找到在完成键入时关闭键盘的最佳方法

我已尝试添加
.ontapsignate
,但它阻止我使用任何选择器。当我点击它们时,什么也没发生

以下是我试图做的:

struct ContentView: View {
    @State var date = Date()

    @State var price = ""
    @State private var tipPercentage = 2

    let tipPercentages = [10, 15, 20, 25, 0]

    var body: some View {
            NavigationView {
                Form {
                    Section {
                        DatePicker(selection: $date, displayedComponents: .date) {
                            Text("Date").bold().foregroundColor(Color.init(red: 100.0/255.0, green: 208.0/255.0, blue: 100.0/255.0))
                        }

                        HStack {
                            Text("Price"))
                            Spacer()
                            TextField("required", text: $price).multilineTextAlignment(.trailing).keyboardType(.decimalPad)
                        }
                    }

                    Section(header: Text("How much tip do you want to leave?")) {
                        Picker("Tip percentage", selection: $tipPercentage) {
                            ForEach(0 ..< tipPercentages.count) {
                                Text("\(self.tipPercentages[$0])%")
                            }
                        }
                        .pickerStyle(SegmentedPickerStyle())
                    }
                }
                .onTapGesture {
                       let keyWindow = UIApplication.shared.connectedScenes
                                          .filter({$0.activationState == .foregroundActive})
                                          .map({$0 as? UIWindowScene})
                                          .compactMap({$0})
                                          .first?.windows
                                          .filter({$0.isKeyWindow}).first
                       keyWindow!.endEditing(true)

                }
            }
    }
}
struct ContentView:View{
@状态变量日期=日期()
@State var price=“”
@国家私有var tipPercentage=2
设TipperContages=[10,15,20,25,0]
var body:一些观点{
导航视图{
形式{
部分{
日期选择器(选择:$date,displayedComponents:.date){
Text(“Date”).bold().foregroundColor(Color.init(红色:100.0/255.0,绿色:208.0/255.0,蓝色:100.0/255.0))
}
HStack{
文本(“价格”))
垫片()
TextField(“必需”,text:$price).multilitextalignment(.training).键盘类型(.decimalPad)
}
}
部分(标题:文本(“您想留下多少小费?”){
选择器(“小费百分比”,选择:$tipPercentage){
ForEach(0..
您需要使用一些UIKit工具来实现这一点

首先,制作一个自定义手势识别器:

    class CloseKeyboardGestureRecognizer: UIGestureRecognizer {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
        // Don't run if the tap is in a TextField, since the keyboard shouldnt hide when tapping a textfield.
        guard let touch = touches.first?.view, touch is UITextField else {
            state = .began
            return
        }
            state = .failed
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
       state = .ended
    }

    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) {
        state = .cancelled
    }
}

extension CloseKeyboardGestureRecognizer: UIGestureRecognizerDelegate {
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}

这将获得在文本字段外点击关闭键盘的行为,而不会影响对其他元素的点击。

删除.ontapsignature并将其添加到表单中:

.gesture(DragGesture().onChanged{_ in UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)})

问题是,当我想滚动到表单底部以填充文本字段时,这是不可能的。。。
.gesture(DragGesture().onChanged{_ in UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)})