在Swift中,如何检查触发TouchsMoved后用户是否不再触摸屏幕?

在Swift中,如何检查触发TouchsMoved后用户是否不再触摸屏幕?,swift,touchesmoved,Swift,Touchesmoved,我需要知道用户开始触摸屏幕,然后知道他什么时候不再触摸屏幕。 通过触摸开始和触摸结束,我可以获得这样的信息。但是,如果用户正在滑动手指,那么我知道他开始使用TouchsMoved进行滑动

我需要知道用户开始触摸屏幕,然后知道他什么时候不再触摸屏幕。 通过触摸开始和触摸结束,我可以获得这样的信息。但是,如果用户正在滑动手指,那么我知道他开始使用TouchsMoved进行滑动<但是,我无法检查他何时不再触摸屏幕。 基本上,用户停止切换后,touchesEnded不会触发。 有什么想法吗

我的代码示例:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {


    if let touch = touches.first {
    print("Finger touched!")
    }
    super.touchesBegan(touches, withEvent:event)
}


override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    if let touch = touches.first {

        print("Finger is swipping!")

    }
    super.touchesBegan(touches, withEvent:event)
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    if let touch = touches.first {

        print("finger is not touching.") //this will not fire if finger stopped swipping

    }
    super.touchesBegan(touches, withEvent:event)
}
覆盖函数触摸开始(触摸:设置,withEvent事件:UIEvent?){
如果让触摸=先触摸{
打印(“手指触摸!”)
}
super.touchsbegind(touchs,withEvent:event)
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
如果让触摸=先触摸{
打印(“手指在摆动!”)
}
super.touchsbegind(touchs,withEvent:event)
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
如果让触摸=先触摸{
print(“手指未触。”)//如果手指停止滑动,则不会触发此命令
}
super.touchsbegind(touchs,withEvent:event)
}
你说过:

print("finger is not touching.") //this will not fire if finger stopped swipping
你说得对,如果手指停止移动,它就不会开火。但是如果手指离开屏幕(停止触摸),它就会触发,这就是你所问的(“不再触摸屏幕”)。

你说:

print("finger is not touching.") //this will not fire if finger stopped swipping

你说得对,如果手指停止移动,它就不会开火。但如果手指离开屏幕(停止触摸),它将触发,这就是您询问的(“不再触摸屏幕”)。

触摸开始:withEvent:
当手指触摸屏幕时,总是会调用一个或多个手指,这些手指被视为同一触摸事件的一部分<代码>触摸移动:withEvent:在事件中的一个或多个手指移动时被触发
touchesEnded:withEvent:
当触摸事件中的一个或多个手指从屏幕上移除时被触发。最后,如果整个事件无效(即取消),则调用
touchscancelled:withEvent:

对于给定的事件,您将始终收到一个或多个对
触摸开始:withEvent:
,可能许多对
触摸移动:withEvent
,然后一个或多个对
触摸结束:withEvent:
触摸取消:withEvent:

这里需要考虑的一些事情:

  • 您应该始终实现这四种方法,即使您什么也不做,以防止部分事件进入响应程序链(超级实现就是这样做的)
  • 如果您正在处理事件,则不应调用super
  • 如果您确实调用super是因为您希望将事件分派到响应程序链的上游,则必须调用正确的super方法(您在
    touchesBegind:withEvent:
    中调用
    touchesMoved:withEvent
    实现中的super上的

  • 为了具体回答您的问题,您可以实现
    touchesend:withEvent
    ,以了解用户何时停止触摸屏幕,其中一个或多个触摸是事件的一部分。

    touchesbeated:withEvent:
    当他们触摸被视为同一事件的一部分的屏幕时,总是会调用一个或多个手指触摸事件。
    touchesMoved:withEvent:
    在事件中的一个或多个手指移动时被触发。
    touchesend:withEvent:
    在触摸事件中的一个或多个手指从屏幕上移除时被触发。最后,如果整个事件无效(即取消),则调用
    touchecancelled:withEvent:

    对于给定的事件,您将始终收到一个或多个对
    触摸开始:withEvent:
    ,可能许多对
    触摸移动:withEvent
    ,然后一个或多个对
    触摸结束:withEvent:
    触摸取消:withEvent:

    这里需要考虑的一些事情:

  • 您应该始终实现这四种方法,即使您什么也不做,以防止部分事件进入响应程序链(超级实现就是这样做的)
  • 如果您正在处理事件,则不应调用super
  • 如果您确实调用super是因为您希望将事件分派到响应程序链的上游,则必须调用正确的super方法(您在
    touchesBegind:withEvent:
    中调用
    touchesMoved:withEvent
    实现中的super上的
  • 为了明确回答您的问题,您可以实施
    touchesend:withEvent
    ,以了解用户何时停止触摸屏幕,其中一个或多个触摸是事件的一部分。

    在Swift 3中工作

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if touches.first != nil {
            print("Finger touched!")
        }
    
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if touches.first != nil {
            print("finger is not touching.")  
        }
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if touches.first != nil {
            print("Touch Move")
        }
    }
    
    override func touchsbegind(touch:Set,带有事件:UIEvent?){
    如果触碰。第一个!=零{
    打印(“手指触摸!”)
    }
    }
    覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
    如果触碰。第一个!=零{
    指纹(“手指不接触”)
    }
    }
    覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
    如果触碰。第一个!=零{
    打印(“触摸移动”)
    }
    }
    
    在Swift 3中工作

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if touches.first != nil {
            print("Finger touched!")
        }
    
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if touches.first != nil {
            print("finger is not touching.")  
        }
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if touches.first != nil {
            print("Touch Move")
        }
    }
    
    override func touchsbegind(touch:Set,带有事件:UIEvent?){
    如果触碰。第一个!=零{
    打印(“手指触摸!”)
    }
    }
    覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
    如果触碰。第一个!=零{
    指纹(“手指不接触”)
    }
    }
    覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
    如果触碰。第一个!=零{
    打印(“触摸移动”)
    }
    }
    
    每次触摸都会得到
    touchesend(:,withEvent:)
    touchecancelled(:,withEvent:)
    。是否覆盖
    touchecancelled(:,withEvent:)
    ?为什么要对所有3种方法调用
    super.touchsbegind
    ?您好。我正在尝试使用“code”override func touchscancell来遵循您的建议