当调用touchesMoved()时,视图滞后一秒钟

时间:2018-03-28 20:00:03

标签: ios swift sprite-kit

当我第一次在下面的代码中移动触摸时,我得到一个长时滞后,这对用户体验不利。

override public  func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

        if gameOverTransitoning {
            return
        }

        if gameState == .waiting || gameState == .gameOver {
            return
        }

        var i = 0
        for touch: AnyObject in touches {

            //allow 2 touches to move, can make the ship move faster
           if i >= 2 {
                break
            }

            let pointOfTouch = touch.location(in: self)
            let previous = touch.previousLocation(in: self)
            let amountDraggedX = pointOfTouch.x - previous.x
            let amountDraggedY = pointOfTouch.y - previous.y

            var x = player.position.x + amountDraggedX
            x = max(player.size.width / 2, x)
            x = min(self.size.width - player.size.width / 2, x)

            var y = player.position.y
            if allowVerticalMove {
                y += amountDraggedY
                y = max(self.size.height * playerMinY, y)
                y = min(self.size.height * playerMaxY, y)
                if i == 0 {
                    let deltaY = y - player.position.y
                    player.accelerate(accelerate: deltaY)
                }
            }

            player.position = CGPoint(x: x, y: y)

            i += 1

        }

    }

}

这是下面的代码。触摸移动功能是SpriteKit中的触摸移动功能

1 个答案:

答案 0 :(得分:0)

首先,我不知道你在做什么:

   player.accelerate(accelerate: deltaY)

您是否尝试在不同的线程上运行此代码?它目前在主线程上运行吗?如果是,那可能是一个原因。