【问题标题】:touchesEnded not called if i do not move finger如果我不移动手指,则不调用 touchesEnded
【发布时间】:2015-02-26 10:43:53
【问题描述】:

我有一段简单的代码,其中我覆盖了 touchesBegan、Ended、Cancelled(空块)和 touchesMoved。 如果我用鼠标点击(我正在台式电脑上测试)touchesBegan 它被调用,但 touchesEnded 仅在我移动手指一段时间时被调用。这使得无法识别手指的单击或拖动,并以不同的方式处理它们。 我不明白这是模拟器问题还是我误解了整个过程。你有同样的问题吗?

我的应用程序有一个简单的解决方案,例如检查 touchesBegan 中的“第一步”变量,但这是一个纯技术问题。

提前谢谢你。

这就是我使用的所有东西,除了 drawRect 这并不重要。 我想这在我的代码中不是问题。

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    let touch = touches.allObjects[0] as UITouch 
    let touchPoint = touch.locationInView(self) 

    println("touchesBegan")

    if (Draw!){
        path.moveToPoint(touchPoint)
        path.addLineToPoint(touchPoint)

        self.setNeedsDisplay()
    }
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
    let touch = touches.allObjects[0] as UITouch
    let touchPoint = touch.locationInView(self)

    println("touchesMoved")

    if (Draw!){
        path.addLineToPoint(touchPoint)
        self.setNeedsDisplay()
    }
}

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
    let touch = touches.allObjects[0] as UITouch
    let touchPoint = touch.locationInView(self)

    println("touchesEnded")

    if (Draw!){
        path.addLineToPoint(touchPoint  
        path = UIBezierPath() // Create new path for next line        
        self.setNeedsDisplay()
    }

}

【问题讨论】:

  • 请您分享您为此使用的代码。

标签: ios ipad swift touches


【解决方案1】:

我很确定这是一个模拟器问题。

请看下面的代码。它对我来说就像一个魅力。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    didMove = false;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    didMove = true;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (didMove == true)
    {
        //finger was moved
        NSLog(@"finger was moved");
    }
    else
    {
        //it's a tap
        NSLog(@"finger not moved it's a tap");
    }

}

在此过程中,我可以将您的注意力转移到 UIGestureRecognizers 上吗?尝试使用它们,因为它们让生活变得轻而易举。

【讨论】:

  • 谢谢,我已经知道 UIGesturesRecognizers 但我只是在练习!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-19
  • 2018-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多