菜鸟教程小白 发表于 2022-12-11 20:07:42

ios - 文本字段上的 UITapGestureRecognizer 与预期不符


                                            <p><p>在我的类里面,我在一个数组 textViewRecognizer 中有 11 个 UITapGestureRecognizer,连接到数组 boxArray 中的 100 个 UITextField 中的 11 个。当点击包含 UIGestureRecognizer 的 Textfield 时,它会运行 tappedTextView 我尝试获取第一响应者的索引。 </p>

<p>但是,由于执行操作的顺序有些奇怪,操作函数只为我提供了前一个第一响应者对刚刚被点击的响应者的第一响应者。 </p>

<p>另外,我必须双击才能选择我想要的文本字段!我需要使用点击功能而不是文本代表,所以这真的很头疼。</p>

<p>我有……</p>

<pre><code>    @objc func tappedTextField(_ sender: UITapGestureRecognizer) {
    for i in 0...99 {
      if (boxArray?.isFirstResponder)! {
            if let index = boxArray.index(of: boxArray) {
            print(index)
            break
            }
      }
    }
}
</code></pre>

<p>在我看来DidLoad 我有</p>

<pre><code>for i in 0...10 {
      textFieldTapRecognizer.addTarget(self, action: #selector(self.tappedTextField(_:)))
}
</code></pre>

<p>在我的课上</p>

<p>我想根据某些条件将 100 个文本字段中的 11 个设置为点击识别器(我将在这里使用常规的 for 循环)</p>

<pre><code>for i in 0...10 {
         boxArray?.addGestureRecognizer(textFieldTapRecognizer)
}
</code></pre>

<ol>
<li><p>在点击后,我是否可以让它给我实际的第一响应者?</p></li>
<li><p>是否可以通过双击来选择具有 UITapGesture 的文本字段?</p></li>
</ol>

<p>非常感谢任何帮助。</p>

<p>已编辑:正确命名的函数</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>听起来您想删除 <code>UITextView</code> 上的自动编辑行为。您可以使用 <code>textViewShouldBeginEditing(_ textView: UITextView) -> Bool</code> <code>UITextViewDelegate</code> 方法,<a href="https://developer.apple.com/documentation/uikit/uitextviewdelegate/1618608-textviewshouldbeginediting" rel="noreferrer noopener nofollow">documented here</a> 获得更多控制权。 .</p>

<p>如果您为该方法返回 <code>false</code>,这应该避免需要双击才能访问您的手势识别器。根据您的用例,您可以通过为您想要实际编辑的 textView 返回 <code>true</code> 来“允许”点击进入 TextView 。</p>

<p>虽然我对您问题的第一响应者部分不是 100% 清楚,但由于 textView 如果没有启动它的编辑模式就不会捕获第一响应者,这应该解决我认为的问题。祝你好运!</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 文本字段上的 UITapGestureRecognizer 与预期不符,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/50177364/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/50177364/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 文本字段上的 UITapGestureRecognizer 与预期不符