ios - 如何避免快速持续触摸检测的延迟?

标签 ios swift core-animation

我想通过连续滑动​​手势控制屏幕上的角色。只要我滑动 +x-x,我的角色就​​会向左或向右移动。只要我握住我的手指,它就会不断地向我滑动的方向移动。当我在手指不离开触摸地面的情况下向与上次滑动相反的方向滑动时,我的角色应该朝这个方向移动。当我从触摸屏上松开手指时,我的角色应该会立即停止。

我的问题是,方向的改变通常会有一个小的延迟。而这种延迟会降低其精确度。

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */
    var player = self.childNodeWithName("man")

    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)
        let touchedNode = self.nodeAtPoint(location)

        lastFingerPosition = location
    }
}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent){
    var player = self.childNodeWithName("man")

    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)
        let touchedNode = self.nodeAtPoint(location)

        if(location.x > lastFingerPosition?.x){
           movePlayer("right")
        } else if(location.x < lastFingerPosition?.x)
           movePlayer("left")
        }
    }
}


override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    var player = self.childNodeWithName("man")

    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)
        let touchedNode = self.nodeAtPoint(location)

        if(touchedNode.name != "buttonFire"){
            player?.removeAllActions()
            lastMoveDirection = ""
         }
    }
}

func movePlayer(direction: String) {
    var player = self.childNodeWithName("man")

    if(lastMoveDirection != direction){
        player?.removeAllActions()
        lastMoveDirection = direction

        var duration:CGFloat? = 0
        var x = player?.position.x
        duration = x!

        if(direction == "left"){
            var xDestination:CGFloat = 10
            var run = SKAction.moveToX(10, duration: NSTimeInterval(Double(duration! / runSpeed)))
            player?.runAction(run, withKey: "moveLeft")
        } else if(direction == "right") {
            duration = self.frame.width - x!
            var xDestination = frame.size.width - 1
            var run = SKAction.moveToX(xDestination, duration: NSTimeInterval(Double(duration! / runSpeed)))
            player?.runAction(run, withKey: "moveRight")
        }
    }
}

最佳答案

我认为这不是程序问题。这更多是控制本身的问题。因为用手指向一个方向滑动,然后向相反方向滑动会导致拇指旋转。

虽然它旋转了一点,但它没有移动,这会导致延迟感。

我想如果你需要非常精确的 Action ,这个控件是没用的——只是因为我们拇指的解剖结构。

关于ios - 如何避免快速持续触摸检测的延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31571174/

相关文章:

iphone - 在 iphone 中将图片上传到 flickr 时出错(Xcode 4.2)

ios - React Native 在 View 的一侧添加边框

ios - 为什么会发生警告?

swift - 关闭 RestKit Swift 中的 WAL 文件模式

ios - 具有 3D 旋转功能的 CAEmitterCell

iphone - 交叉溶解可以在 iPhone 上运行,但不能在 iPad 上运行

iOS 获取 UITableView 可见部分的屏幕截图

ios - 如何在代码中创建具有两个尺寸类的 UITraitCollection?

uiview - 如何通过触摸移动 UIView 使其保持在圆圈内?

ios - 动画工具栏中的完整更改