菜鸟教程小白 发表于 2022-12-11 18:25:21

ios - 识别器位置(在 : recognizer. View 中)快速返回零点


                                            <p><p>我对 Swift 中的 UIPanGestureRecognizer 有一个奇怪的案例。
我有一个处理平移手势的函数,并在 UIGestureRecognizerDelegate 中指定“false”,这样没有其他手势会干扰平移。</p>

<p>这是有问题的案例:</p>

<ol>
<li>用户用一根手指触摸并开始平移</li>
<li>用户将第二根手指放在屏幕上 - 第二根手指被忽略</li>
<li>用户抬起第一根手指,而第二根手指仍在触摸 - 此时我的处理程序被调用
<code>recognizer.state == .ended</code>。问题是此时的位置(我通过调用 <code>recognizer.location(in:recognizer.view)</code> 返回点 <code>(0,0)</code><</li>
</ol>

<p>我是否使用了错误的方法来理解这一点?似乎由于第二根手指被完全忽略了,第一根手指应该表现得像一个普通的平移,当状态==结束时我会得到位置。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我刚刚对其进行了测试 - 识别器结束时位置不再可用(使用以下方法)</p>

<pre><code>recognizer.location(ofTouch: 0, in: recognizer.view)
</code></pre>

<p>我会添加一个变量来跟踪 .began 和 .changed 中的最后一个已知点(我在其中获取数据)然后在 .ended 中使用该数据</p>

<p>编辑:通过子类化获取数据</p>

<pre><code>class pgr: UIPanGestureRecognizer {
    var myTouch:Set&lt;UITouch&gt;!

    override func touchesEnded(_ touches: Set&lt;UITouch&gt;, with event: UIEvent) {
      myTouch = touches
      super.touchesEnded(touches, with: event)
    }

}
</code></pre>

<p>然后回到你的实现中</p>

<pre><code>    if sender.state == .ended {
      print(sender.myTouch.first?.location(in: sender.view))
    }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 识别器位置(在 : recognizer.View 中)快速返回零点,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45441545/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45441545/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 识别器位置(在 : recognizer. View 中)快速返回零点