How to recognize continuous touch in Swift?

后端 未结 3 1992
余生分开走
余生分开走 2021-02-03 11:59

How can I recognize continuous user touch in Swift code? By continuous I mean that the user has her finger on the screen. I would like to move a sprite kit node to the direction

3条回答
  •  半阙折子戏
    2021-02-03 12:27

    Below is the code to drag the node around on X position (left and right), it is very easy to add Y position and do the same thing.

    let item = SKSpriteNode(imageNamed: "xx")
    var itemXposition = 50
    
    override func touchesMoved(_ touches: Set, with event: UIEvent?) {
    // updates itemXposition variable on every touch
        for touch in touches {
            let location = touch.location(in: self)
            itemXposition = Int(location.x)
        }
    }
    
    // this function is called for each frame render, updates the position on view
    override func update(_ currentTime: TimeInterval) {
        spaceShip.position = CGPoint(x: self.itemXposition , y: 50 )
    }
    

提交回复
热议问题