首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >onTapGesture禁用我的机械臂和分段tap

onTapGesture禁用我的机械臂和分段tap
EN

Stack Overflow用户
提问于 2020-04-16 04:25:32
回答 2查看 718关注 0票数 1

我正在使用SwiftUI创建一个Form。在我的Form中,我有一个DatePickerTextField和一个SegmentedPickerStyle

我的TextField正在使用.decimalPad,我正在尝试找出在完成输入时不使用键盘的最佳方法。

我已经尝试添加一个.onTapGesture,但它阻止我使用我的任何选取器。当我点击它们时,没有任何反应。

这就是我想要做的:

代码语言:javascript
复制
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)

                }
            }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-16 08:05:13

删除您的.onTapGesture并将其添加到表单中:

代码语言:javascript
复制
.gesture(DragGesture().onChanged{_ in UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)})
票数 3
EN

Stack Overflow用户

发布于 2020-04-16 06:35:21

你会想用一些UIKit的东西来做这件事。

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

代码语言:javascript
复制
    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
    }
}

然后在SceneDelegate的场景(_:willConnectTo:options:)中,添加:

代码语言:javascript
复制
        let gesture = CloseKeyboardGestureRecognizer(target: window, action:#selector(UIView.endEditing))
        gesture.delegate = gesture
        gesture.cancelsTouchesInView = false
        gesture.requiresExclusiveTouchType = false

        window.addGestureRecognizer(gesture)
    }

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61237991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档