菜鸟教程小白 发表于 2022-12-13 02:28:29

ios - 仅检测一次双击手势


                                            <p><pre><code>- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *aTouch in touches) {
      if (aTouch.tapCount &gt;= 2) {
            // The view responds to the tap
      }
    }
}
</code></pre>

<p>我正在使用上面的代码来检测双击手势;但是,如何将代码设置为只发生一次?</p>

<p>换句话说,当您<strong>点击一次</strong>时,角色就会跳跃。当您快速连续点击两次时,角色将进行二段跳。但是,如何设置点按,以使角色不会在没有最初<strong>点按一次</strong>的情况下连续双跳并从单 View 跳得更高?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>实现这一点的一个非常简单的方法是声明一个全局 <code>bool</code> 变量,并在检测到双击后设置其值! </p>

<p>类似这样的:</p>

<pre><code>@interface MyViewController()
{
    bool isTapped;
}
@end

@implementation MyViewController
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *aTouch in touches) {
      if (aTouch.tapCount &gt;= 2) {
            if(!isTapped) {
                // The view responds to the tap
                isTapped = YES;
            }
      }
    }
}
@end
</code></pre>

<p>希望对你有帮助</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 仅检测一次双击手势,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/26195349/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/26195349/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 仅检测一次双击手势