如何避免swift中持续触摸检测的延迟?

时间:2015-07-22 18:27:51

标签: 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")
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

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

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