简体   繁体   中英

swipe gesture in touches methods in swift

So I already have gesture recongizers implemented into my game to detect the movement of my player but have found them to be not giving me the result I want, so I am looking at making the swipe gesture in the touches method and the taps also in the touches method. I have managed to make the tap functionality work in the touches method but I'm unable to implement the ability for swiping in the touches method and I can't seem to find a tutorial on how to do this. My code below shows the touches methods I'm using to try and achieve this:

class GameScene: SKScene {

 var touchOrigin = CGPoint()
 var player = SKSpriteNode()


override func didMove(to view: SKView) {

    backgroundColor = .black

    player = SKSpriteNode(texture: nil, color: .orange, size: CGSize(width: 50, height: 50))
    player.position = CGPoint(x: 0, y: 0)
    addChild(player)

}

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

}

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

}

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

    for touch in touches {

        var currentTouchPosition = touch.location(in: self)
        touchOrigin = touch.location(in: self)

        if (Int(touchOrigin.x) > Int(currentTouchPosition.x)) {

            player.position.x -= 50

        } else if (Int(touchOrigin.x) < Int(currentTouchPosition.x)) {

            player.position.x += 50

        }

        if touch.tapCount == 1 {

            player.position.y += 50 //replace this with function :)

        } else if touch.tapCount >= 2 {

            player.position.y += 150 // change to run shield effect :)

        }
    }

}

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

}

How can I make the touches method recognise a swipe gesture in a certain direction? and if they swipe in a direction and not take their finger off the screen and swipe back to the origin point in one motion how can i make it so it is then recognised as a tap instead?

Here's an example of how to detect a swipe gesture.

First, define instance variables to store the starting location and time

var start:(location:CGPoint, time:TimeInterval)?

and define parameters of the swipe gesture. Adjust these accordingly:

let minDistance:CGFloat = 25
let minSpeed:CGFloat = 1000
let maxSpeed:CGFloat = 6000

In touchesBegan , save the location/time of each new touch event

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        start = (touch.location(in:self), touch.timestamp)
    }
}

In touchesEnded , determine if the user's gesture was a swipe by comparing the distance and speed of the gesture. The tests for diagonal swipes are optional.

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    var swiped = false
    if let touch = touches.first, let startTime = self.start?.time,
            let startLocation = self.start?.location {
        let location = touch.location(in:self)
        let dx = location.x - startLocation.x
        let dy = location.y - startLocation.y
        let distance = sqrt(dx*dx+dy*dy)

        // Check if the user's finger moved a minimum distance
        if distance > minDistance {
            let deltaTime = CGFloat(touch.timestamp - startTime)
            let speed = distance / deltaTime

            // Check if the speed was consistent with a swipe
            if speed >= minSpeed && speed <= maxSpeed {

                // Determine the direction of the swipe
                let x = abs(dx/distance) > 0.4 ? Int(sign(Float(dx))) : 0
                let y = abs(dy/distance) > 0.4 ? Int(sign(Float(dy))) : 0

                swiped = true
                switch (x,y) {
                case (0,1):
                    print("swiped up")
                case (0,-1):
                    print("swiped down")
                case (-1,0):
                    print("swiped left")
                case (1,0):
                    print("swiped right")
                case (1,1):
                    print("swiped diag up-right")
                case (-1,-1):
                    print("swiped diag down-left")
                case (-1,1):
                    print("swiped diag up-left")
                case (1,-1):
                    print("swiped diag down-right")
                default:
                    swiped = false
                    break
                }
            }
        }
    }
    start = nil
    if !swiped {
        // Process non-swipes (taps, etc.)
        print("not a swipe")
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM