我想通过连续滑动手势控制屏幕上的角色。只要我滑动 +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/