How to recognize continuous touch in Swift?

后端 未结 3 1923
余生分开走
余生分开走 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:30

    The basic steps

    1. Store the location of the touch events (touchesBegan/touchesMoved)
    2. Move sprite node toward that location (update)
    3. Stop moving the node when touch is no longer detected (touchesEnded)

    Here's an example of how to do that

    Xcode 8

    let sprite = SKSpriteNode(color: SKColor.white, size: CGSize(width:32, height:32))
    var touched:Bool = false
    var location = CGPoint.zero
    
    override func didMove(to view: SKView) {
        /* Add a sprite to the scene */
        sprite.position = CGPoint(x:0, y:0)
        self.addChild(sprite)
    }
    
    override func touchesBegan(_ touches: Set, with event: UIEvent?) {
        touched = true
        for touch in touches {
            location = touch.location(in:self)
        }
    }
    
    override func touchesMoved(_ touches: Set, with event: UIEvent?) {
        for touch in touches {
            location = touch.location(in: self)
        }
    }
    
    override func touchesEnded(_ touches: Set, with event: UIEvent?) {
        // Stop node from moving to touch
        touched = false
    }
    
    override func update(_ currentTime: TimeInterval) {
        // Called before each frame is rendered
        if (touched) {
            moveNodeToLocation()
        }
    }
    
    // Move the node to the location of the touch
    func moveNodeToLocation() {
        // Compute vector components in direction of the touch
        var dx = location.x - sprite.position.x
        var dy = location.y - sprite.position.y
        // How fast to move the node. Adjust this as needed
        let speed:CGFloat = 0.25
        // Scale vector
        dx = dx * speed
        dy = dy * speed
        sprite.position = CGPoint(x:sprite.position.x+dx, y:sprite.position.y+dy)
    }
    

    Xcode 7

    let sprite = SKSpriteNode(color: SKColor.whiteColor(), size: CGSizeMake(32, 32))
    var touched:Bool = false
    var location = CGPointMake(0, 0)
    
    override func didMoveToView(view: SKView) {
        self.scaleMode = .ResizeFill
        /* Add a sprite to the scene */
        sprite.position = CGPointMake(100, 100)
        self.addChild(sprite)
    }
    
    override func touchesBegan(touches: Set, withEvent event: UIEvent?) {
        /* Start moving node to touch location */
        touched = true
        for touch in touches {
            location = touch.locationInNode(self)
        }
    }
    
    override func touchesMoved(touches: Set, withEvent event: UIEvent?) {
        /* Update to new touch location */
        for touch in touches {
            location = touch.locationInNode(self)
        }
    }
    
    override func touchesEnded(touches: Set, withEvent event: UIEvent?) {
        // Stop node from moving to touch
        touched = false
    }
    
    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
        if (touched) {
            moveNodeToLocation()
        }
    }
    
    // Move the node to the location of the touch
    func moveNodeToLocation() {
        // How fast to move the node
        let speed:CGFloat = 0.25
        // Compute vector components in direction of the touch
        var dx = location.x - sprite.position.x
        var dy = location.y - sprite.position.y
        // Scale vector
        dx = dx * speed
        dy = dy * speed
        sprite.position = CGPointMake(sprite.position.x+dx, sprite.position.y+dy)
    
    }
    

提交回复
热议问题