菜鸟教程小白 发表于 2022-12-12 03:54:52

ios - 调用 touchescancelled 而不是 touchesended


                                            <p><p>我试图在触摸时点亮一个 View ,并在触摸结束时取消它。这样做时,我注意到当我从 View 中抬起手指时, <code>- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event</code> 方法没有被调用,而是 <code> - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event</code> 方法在 View 上被调用。这是正常的行为吗?我认为如果由于系统问题(如内存不足警告)而取消触摸,则会调用 <code>touchesCancelled</code>。我通过使用 touchesCancelled 完成了我的工作,但我想知道为什么它不能正确调用 <code>touchesEnded</code>。是bug吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这不是正常行为。抬起手指时,应该调用 touchesEnded。 </p>

<p>当您的 View 在触摸期间被移除或发生系统事件(例如,当您的手指触摸屏幕时锁定手机)时,应该调用touchesCancelled</p>

<p>参见 <a href="https://developer.apple.com/documentation/uikit/uiresponder/1621084-touchesended" rel="noreferrer noopener nofollow">touchesEnded</a> 的文档和 <a href="https://developer.apple.com/documentation/uikit/uiresponder/1621116-touchescancelled" rel="noreferrer noopener nofollow">touchesCancelled</a> .</p>

<p>没有您的代码,就不可能知道发生了什么。但是,即使您设法按应有的方式调用了 touchEnded,您似乎也希望对这两种情况都做出响应。迅速:</p>

<pre class="lang-swift prettyprint-override"><code>class MyView: UIView {
    override func touchesEnded(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
      stopGlow()
    }

    override func touchesCancelled(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
      stopGlow()
    }

    private func stopGlow() {
      //Your code here...
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 调用 touchescancelled 而不是 touchesended,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/10325776/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/10325776/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 调用 touchescancelled 而不是 touchesended