菜鸟教程小白 发表于 2022-12-12 15:09:13

ios - 在 UITextView 中禁用点击并按住


                                            <p><p>有没有办法在 UITextView 中禁用点击<strong>并按住</strong>?</p>

<p>我目前有一个 UITextView 的子类,它实现:</p>

<ul>
<li><p><strong><code>caretRectForPosition:</code></strong>,它总是返回 <code>CGRectZero</code> 来隐藏插入符号</p></li>
<li><p><strong><code>addGestureRecognizer:</code></strong>,实现禁用<code>UITapGestureRecognizer</code>和<code>UILongPressGestureRecognizer</code>手势</p> </li>
</ul>

<p><strong>除了这些方法之外,我还实现了 <code>-canPerformAction:withSender:</code>,它只返回 <code>NO</code>,但是这个方法甚至没有被调用(我已经在那里放了一个断点)。</strong></p>

<p>现在一切正常,我需要它来工作,但遗憾的是用户仍然可以<strong>点击并按住</strong>文本字段。这会导致出现放大镜,并使文本字段可选。我已经用 <code></code> 记录了附加的手势,并收到了以下数组:</p>

<pre><code>&lt;__NSArrayI 0x18e50b10&gt;(
&lt; UITapGestureRecognizer: 0x18f60a80; state = Possible; enabled = NO; view = &lt;CDICTextField 0x18f55ff0&gt;; target= &lt;(action=pinFieldTapped:, target=&lt;CDICPINWizardViewController 0x18f53ee0&gt;)&gt;&gt;,
&lt; UITextTapRecognizer: 0x18e344d0; state = Possible; enabled = NO; delaysTouchesEnded = NO; view = &lt;CDICTextField 0x18f55ff0&gt;; target= &lt;(action=oneFingerTripleTap:, target=&lt;UITextInteractionAssistant   0x18e34440&gt;)&gt;; numberOfTapsRequired = 3&gt;,
&lt; UITextTapRecognizer: 0x18e34610; state = Possible; enabled = NO; delaysTouchesEnded = NO; view = &lt;CDICTextField 0x18f55ff0&gt;; target= &lt;(action=oneFingerDoubleTap:, target=&lt;UITextInteractionAssistant   0x18e34440&gt;)&gt;; numberOfTapsRequired = 2&gt;,
&lt; UITextTapRecognizer: 0x18e34740; state = Possible; enabled = NO; delaysTouchesEnded = NO; view = &lt;CDICTextField 0x18f55ff0&gt;; target= &lt;(action=twoFingerSingleTap:, target=&lt;UITextInteractionAssistant   0x18e34440&gt;)&gt;; numberOfTouchesRequired = 2&gt;,
&lt; UITapAndAHalfRecognizer: 0x18e34870; state = Possible; view = &lt;CDICTextField 0x18f55ff0&gt;; target= &lt;(action=tapAndAHalf:, target=&lt;UITextInteractionAssistant 0x18e34440&gt;)&gt;&gt;,
&lt; UILongPressGestureRecognizer: 0x18e33830; state = Possible; enabled = NO; delaysTouchesEnded = NO; view = &lt;CDICTextField 0x18f55ff0&gt;; target= &lt;(action=twoFingerRangedSelectGesture:, target=&lt;    UITextInteractionAssistant 0x18e34440&gt;)&gt;&gt;,
&lt; UITextTapRecognizer: 0x18e34a70; state = Possible; enabled = NO; delaysTouchesEnded = NO; view = &lt;CDICTextField 0x18f55ff0&gt;; target= &lt;(action=oneFingerTap:, target=&lt;UITextInteractionAssistant   0x18e34440&gt;)&gt;&gt;,
&lt; UIVariableDelayLoupeGesture: 0x18e34b90; state = Possible; enabled = NO; delaysTouchesEnded = NO; view = &lt;CDICTextField 0x18f55ff0&gt;; target= &lt;(action=loupeGesture:, target=&lt;UITextInteractionAssistant   0x18e34440&gt;)&gt;&gt;
)
</code></pre>

<p>检查数组我确实看到所有手势报告<code>enabled = NO</code>,除了<code>UITapAndAHalfRecognizer</code>(它甚至不是我们开发人员可见的类)。
现在我不确定那个手势是否是罪魁祸首。如果是这样,我也不知道如何禁用它。</p>

<p>我已经搜索过答案,但找不到答案。此外, <code>-canPerformAction:withSender:</code> 没有被调用,这可能是因为没有执行任何操作。 <code>UIMenuViewController</code> 也不可见(我没有看到带有 <em>Select/Copy/Paste</em> 等的黑条),只是一个放大镜。</p>

<p>UITextField所在的<code>UIViewController</code>实现:</p>

<hr/>

<p>再一次,我<em>做</em>想要的是:</p>

<ul>
<li>使 UITextfield 可编辑,但不可选择</li>
<li>禁用所有手势(双击、点按并按住、长按)</li>
</ul>

<p>我<em>不</em>想要的是:</p>

<ul>
<li>响应选择/复制/粘贴等的 UITextField。</li>
<li>以任何方式展示放大镜</li>
</ul>

<p>“为什么”的原因是我正在制作一种 PIN 屏幕,我希望用户在我想要的字段中输入一个数字(因为我必须使用文本字段,请不要提供任何其他创建 PIN 屏幕的方式)。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您应该继承 <code>UITextView</code> 并覆盖 <code>canPerformAction:withSender</code>。不应提供复制/粘贴的文本字段应使用您的子类定义。</p>

<p><strong>NonCopyPasteField.h:</strong></p>

<pre><code>@interface NonCopyPasteField : UITextField
@end
</code></pre>

<p><strong>NonCopyPasteField.m:</strong></p>

<pre><code>@implemetation
(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(copy:) || action == @selector(paste:)) {
      return NO;
    }
    ;
}
@end
</code></pre>

<p>如果您需要有关如何实现它的任何帮助,请告诉我们。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 UITextView 中禁用点击并按住,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/29985090/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/29985090/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 UITextView 中禁用点击并按住